Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
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
PSDI Army
yii2
Commits
7a937903
Commit
7a937903
authored
Jul 11, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
`yii\mongodb\Collection::buildLikeCondition()` fixed to escape regular expression.
`yii\mongodb\Collection::buildRegexCondition()` added.
parent
3e3ae634
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
3 deletions
+37
-3
Collection.php
extensions/mongodb/Collection.php
+23
-1
QueryRunTest.php
tests/unit/extensions/mongodb/QueryRunTest.php
+14
-2
No files found.
extensions/mongodb/Collection.php
View file @
7a937903
...
@@ -832,6 +832,7 @@ class Collection extends Object
...
@@ -832,6 +832,7 @@ class Collection extends Object
'NOT BETWEEN'
=>
'buildBetweenCondition'
,
'NOT BETWEEN'
=>
'buildBetweenCondition'
,
'IN'
=>
'buildInCondition'
,
'IN'
=>
'buildInCondition'
,
'NOT IN'
=>
'buildInCondition'
,
'NOT IN'
=>
'buildInCondition'
,
'REGEX'
=>
'buildRegexCondition'
,
'LIKE'
=>
'buildLikeCondition'
,
'LIKE'
=>
'buildLikeCondition'
,
];
];
...
@@ -999,6 +1000,27 @@ class Collection extends Object
...
@@ -999,6 +1000,27 @@ class Collection extends Object
}
}
/**
/**
* Creates a Mongo regular expression condition.
* @param string $operator the operator to use
* @param array $operands the first operand is the column name.
* The second operand is a single value that column value should be compared with.
* @return array the generated Mongo condition.
* @throws InvalidParamException if wrong number of operands have been given.
*/
public
function
buildRegexCondition
(
$operator
,
$operands
)
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
]))
{
throw
new
InvalidParamException
(
"Operator '
$operator
' requires two operands."
);
}
list
(
$column
,
$value
)
=
$operands
;
if
(
!
(
$value
instanceof
\MongoRegex
))
{
$value
=
new
\MongoRegex
(
$value
);
}
return
[
$column
=>
$value
];
}
/**
* Creates a Mongo condition, which emulates the `LIKE` operator.
* Creates a Mongo condition, which emulates the `LIKE` operator.
* @param string $operator the operator to use
* @param string $operator the operator to use
* @param array $operands the first operand is the column name.
* @param array $operands the first operand is the column name.
...
@@ -1013,7 +1035,7 @@ class Collection extends Object
...
@@ -1013,7 +1035,7 @@ class Collection extends Object
}
}
list
(
$column
,
$value
)
=
$operands
;
list
(
$column
,
$value
)
=
$operands
;
if
(
!
(
$value
instanceof
\MongoRegex
))
{
if
(
!
(
$value
instanceof
\MongoRegex
))
{
$value
=
new
\MongoRegex
(
$value
);
$value
=
new
\MongoRegex
(
'/'
.
preg_quote
(
$value
)
.
'/'
);
}
}
return
[
$column
=>
$value
];
return
[
$column
=>
$value
];
...
...
tests/unit/extensions/mongodb/QueryRunTest.php
View file @
7a937903
...
@@ -123,7 +123,7 @@ class QueryRunTest extends MongoDbTestCase
...
@@ -123,7 +123,7 @@ class QueryRunTest extends MongoDbTestCase
->
where
([
->
where
([
'name'
=>
[
'name1'
,
'name5'
,
'name10'
]
'name'
=>
[
'name1'
,
'name5'
,
'name10'
]
])
])
->
andWhere
([
'LIKE'
,
'name'
,
'
/me1/
'
])
->
andWhere
([
'LIKE'
,
'name'
,
'
me1
'
])
->
andWhere
([
'name'
=>
'name10'
])
->
andWhere
([
'name'
=>
'name10'
])
->
all
(
$connection
);
->
all
(
$connection
);
$this
->
assertEquals
(
1
,
count
(
$rows
));
$this
->
assertEquals
(
1
,
count
(
$rows
));
...
@@ -180,12 +180,24 @@ class QueryRunTest extends MongoDbTestCase
...
@@ -180,12 +180,24 @@ class QueryRunTest extends MongoDbTestCase
$this
->
assertEquals
(
1
,
count
(
$rows
));
$this
->
assertEquals
(
1
,
count
(
$rows
));
}
}
public
function
testRegex
()
{
$connection
=
$this
->
getConnection
();
$query
=
new
Query
;
$rows
=
$query
->
from
(
'customer'
)
->
where
([
'REGEX'
,
'name'
,
'/me1/'
])
->
all
(
$connection
);
$this
->
assertEquals
(
2
,
count
(
$rows
));
$this
->
assertEquals
(
'name1'
,
$rows
[
0
][
'name'
]);
$this
->
assertEquals
(
'name10'
,
$rows
[
1
][
'name'
]);
}
public
function
testLike
()
public
function
testLike
()
{
{
$connection
=
$this
->
getConnection
();
$connection
=
$this
->
getConnection
();
$query
=
new
Query
;
$query
=
new
Query
;
$rows
=
$query
->
from
(
'customer'
)
$rows
=
$query
->
from
(
'customer'
)
->
where
([
'LIKE'
,
'name'
,
'
/me1/
'
])
->
where
([
'LIKE'
,
'name'
,
'
me1
'
])
->
all
(
$connection
);
->
all
(
$connection
);
$this
->
assertEquals
(
2
,
count
(
$rows
));
$this
->
assertEquals
(
2
,
count
(
$rows
));
$this
->
assertEquals
(
'name1'
,
$rows
[
0
][
'name'
]);
$this
->
assertEquals
(
'name1'
,
$rows
[
0
][
'name'
]);
...
...
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