Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
0819ef53
Commit
0819ef53
authored
Aug 11, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweak regexp operations, fixes #282
parent
5f755fea
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
3 deletions
+26
-3
Builder.php
src/Jenssegers/Mongodb/Query/Builder.php
+20
-3
QueryBuilderTest.php
tests/QueryBuilderTest.php
+6
-0
No files found.
src/Jenssegers/Mongodb/Query/Builder.php
View file @
0819ef53
...
@@ -832,13 +832,30 @@ class Builder extends QueryBuilder {
...
@@ -832,13 +832,30 @@ class Builder extends QueryBuilder {
$operator
=
'='
;
$operator
=
'='
;
$regex
=
str_replace
(
'%'
,
''
,
$value
);
$regex
=
str_replace
(
'%'
,
''
,
$value
);
//
Prepare regex
//
Convert like to regular expression.
if
(
s
ubstr
(
$value
,
0
,
1
)
!=
'%'
)
$regex
=
'^'
.
$regex
;
if
(
s
tarts_with
(
$value
,
'%'
)
)
$regex
=
'^'
.
$regex
;
if
(
substr
(
$value
,
-
1
)
!=
'%'
)
$regex
=
$regex
.
'$'
;
if
(
ends_with
(
$value
,
'%'
)
)
$regex
=
$regex
.
'$'
;
$value
=
new
MongoRegex
(
"/
$regex
/i"
);
$value
=
new
MongoRegex
(
"/
$regex
/i"
);
}
}
// Manipulate regexp operations.
elseif
(
in_array
(
$operator
,
array
(
'regexp'
,
'not regexp'
,
'regex'
,
'not regex'
)))
{
// Automatically convert regular expression strings to MongoRegex objects.
if
(
!
$value
instanceof
MongoRegex
)
{
$value
=
new
MongoRegex
(
$value
);
}
// For inverse regexp operations, we can just use the $not operator
// and pass it a MongoRegex instence.
if
(
starts_with
(
$operator
,
'not'
))
{
$operator
=
'not'
;
}
}
if
(
!
isset
(
$operator
)
or
$operator
==
'='
)
if
(
!
isset
(
$operator
)
or
$operator
==
'='
)
{
{
$query
=
array
(
$column
=>
$value
);
$query
=
array
(
$column
=>
$value
);
...
...
tests/QueryBuilderTest.php
View file @
0819ef53
...
@@ -540,6 +540,12 @@ class QueryBuilderTest extends TestCase {
...
@@ -540,6 +540,12 @@ class QueryBuilderTest extends TestCase {
$results
=
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'REGEX'
,
$regex
)
->
get
();
$results
=
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'REGEX'
,
$regex
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$results
));
$this
->
assertEquals
(
2
,
count
(
$results
));
$results
=
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'regexp'
,
'/.*doe/i'
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$results
));
$results
=
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'not regexp'
,
'/.*doe/i'
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$results
));
DB
::
collection
(
'users'
)
->
insert
(
array
(
DB
::
collection
(
'users'
)
->
insert
(
array
(
array
(
array
(
'name'
=>
'John Doe'
,
'name'
=>
'John Doe'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment