thinkphp using MONGODB database to implement multi-criteria Query method _php instance

Source: Internet
Author: User
Tags explode mongodb

There is a project using the MongoDB database, query conditions have and also, according to thinkphp official manual, using a composite query (_complex), Getlastsql output query statement, found that the query condition is empty. Query with string mode (_string), The request string query (_query) does not meet the requirements. It is estimated that the number of MongoDB users is low and thinkphp official support for this is insufficient. Open the MongoDB drive of thinkphp, 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, When thinkphp uses MongoDB, it does not support composite queries at all. Plus:

Copy 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 to convert to objects is because using thinkphp uses the Json_encode function to generate the query statement, but if the array element takes the Key,json_encode function to convert the array into an object form, the MongoDB is not recognized. Because you currently use only or, so , the code only handles the OR.
Also, find a bug (not counted) in the Parsewhere method:

Copy Code code as follows:

foreach ($where as $key => $val) {
if (' _id '!= $key && 0===strpos ($key, ' _ ')) {
Parsing Special Conditional expressions
The 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), and an error occurs when the special expression is not the first element in the where array, and the $query array in the else's code. All gone.

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.