There is a project with the MongoDB database, query conditions have and also have or, press thinkphp official manual, using compound query (_complex), Getlastsql output query statement, found that the query condition is empty. Query with string pattern (_string), The request string query (_query) does not meet the requirements. There are few users in MongoDB, thinkphp official support is not enough. Open thinkphp MongoDB driver, thinkphp/extend/driver/db/ DbMongo.class.php, find protected function Parsethinkwhere ($key, $val) method, you can find that there is no _complex in switch, that is to say, thinkphp when using MongoDB, compound queries are not supported at all. Plus:
Copy the Code code as follows:
Case ' _complex '://Compound Query
$arr = Array ();
foreach ($val as $nkey = = $nval) {
if (Strpos ($nkey, ' _ ')!=0)
{
$PARSEARR = $this->parsewhereitem ($nkey, $nval);
Convert to Object
$obj =new StdClass ();
foreach ($parseArr as $pkey = $pval)
{
$obj $pkey = $pval;
}
Array_push ($arr, $obj);
}
}
if (Isset ($val [' _logic ']) && strtolower ($val [' _logic ']) = = = ' or ') {
unset ($val [' _logic ']);
$query [' $or '] = $arr;
}
Break
The reason here is to convert to an object is because using thinkphp to generate a query statement using the Json_encode function, but if the array element with the Key,json_encode function will convert the array into the form of an object, MongoDB does not recognize it. Because currently only use or, so , the code only handles or.
In addition, find a bug (do not know count), in the Parsewhere method:
Copy CodeThe code is as follows:
foreach ($where as $key = = $val) {
if (' _id '! = $key && 0===strpos ($key, ' _ ')) {
Parsing Special Conditional expressions
Original $query = $this->parsethinkwhere ($key, $val);
$query = Array_merge ($query, $this->parsethinkwhere ($key, $val));
}else{
Security filtering for query fields
if (!preg_match ('/^[a-z_\|\&\-.a-z0-9]+$/', trim ($key))) {
Throw_exception (L (' _error_query_ '). ': '. $key);
}
$key = Trim ($key);
if (Strpos ($key, ' | ')) {
$array = Explode (' | ', $key);
$str = Array ();
foreach ($array as $k) {
$str [] = $this->parsewhereitem ($k, $val);
}
$query [' $or '] = $str;
}elseif (Strpos ($key, ' & ')) {
$array = Explode (' & ', $key);
$str = Array ();
foreach ($array as $k) {
$str [] = $this->parsewhereitem ($k, $val);
}
$query = Array_merge ($query, $STR);
}else{
$str = $this->parsewhereitem ($key, $val);
$query = Array_merge ($query, $STR);
}
}
}
When parsing a special conditional expression, the source code is $query= $this->parsethinkwhere ($key, $val); When the special expression is not the first element in the where array, an error is made, and the code in else gets the $query array, It's gone.
http://www.bkjia.com/PHPjc/824792.html www.bkjia.com true http://www.bkjia.com/PHPjc/824792.html techarticle there is a project with the MongoDB database, query conditions have and also have or, press thinkphp official manual, using compound query (_complex), Getlastsql output query statement, found that the query condition is empty. The word ...