thinkphp _php tutorial on using MONGODB database to implement multi-conditional Query method

Source: Internet
Author: User
Tags mongodb driver
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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.