Skip to content

PHPORM-232 Support whereLike and whereNotLike #3108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [4.8.0] - next

* Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550)
* Add `Query\Builder::whereLike()` and `whereNotLike()` methods by @GromNaN in [#3108](https://github.com/mongodb/laravel-mongodb/pull/3108)
* Deprecate `Connection::collection()` and `Schema\Builder::collection()` methods by @GromNaN in [#3062](https://github.com/mongodb/laravel-mongodb/pull/3062)
* Deprecate `Model::$collection` property to customize collection name. Use `$table` instead by @GromNaN in [#3064](https://github.com/mongodb/laravel-mongodb/pull/3064)

Expand Down
10 changes: 9 additions & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,8 @@ protected function compileWhereBasic(array $where): array
// All backslashes are converted to \\, which are needed in matching regexes.
preg_quote($value),
);
$value = new Regex('^' . $regex . '$', 'i');
$flags = $where['caseSensitive'] ?? false ? '' : 'i';
$value = new Regex('^' . $regex . '$', $flags);

// For inverse like operations, we can just use the $not operator with the Regex
$operator = $operator === 'like' ? '=' : 'not';
Expand Down Expand Up @@ -1324,6 +1325,13 @@ protected function compileWhereNotIn(array $where): array
return [$where['column'] => ['$nin' => array_values($where['values'])]];
}

protected function compileWhereLike(array $where): array
{
$where['operator'] = $where['not'] ? 'not like' : 'like';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return $this->compileWhereBasic($where);
}

protected function compileWhereNull(array $where): array
{
$where['operator'] = '=';
Expand Down
30 changes: 30 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,36 @@ function (Builder $builder) {
fn (Builder $builder) => $builder->where('name', 'like', '_ac__me_'),
];

yield 'whereLike' => [
['find' => [['name' => new Regex('^1$', 'i')], []]],
fn (Builder $builder) => $builder->whereLike('name', '1'),
];

yield 'whereLike case not sensitive' => [
['find' => [['name' => new Regex('^1$', 'i')], []]],
fn (Builder $builder) => $builder->whereLike('name', '1', false),
];

yield 'whereLike case sensitive' => [
['find' => [['name' => new Regex('^1$', '')], []]],
fn (Builder $builder) => $builder->whereLike('name', '1', true),
];

yield 'whereNotLike' => [
['find' => [['name' => ['$not' => new Regex('^1$', 'i')]], []]],
fn (Builder $builder) => $builder->whereNotLike('name', '1'),
];

yield 'whereNotLike case not sensitive' => [
['find' => [['name' => ['$not' => new Regex('^1$', 'i')]], []]],
fn (Builder $builder) => $builder->whereNotLike('name', '1', false),
];

yield 'whereNotLike case sensitive' => [
['find' => [['name' => ['$not' => new Regex('^1$', '')]], []]],
fn (Builder $builder) => $builder->whereNotLike('name', '1', true),
];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


$regex = new Regex('^acme$', 'si');
yield 'where BSON\Regex' => [
['find' => [['name' => $regex], []]],
Expand Down
Loading