thinkphp5̽ÌÖÔõÑùÅжÏMQL¹¤¾ßÊÇ·ñΪ¿Õ
ÔÚʹÓÃthinkphp5ÖеÄmql¹¤¾ßʱ£¬ÎÒÃÇÓÐʱÐèÒªÅжÏÕâ¸ö¹¤¾ßÊÇ·ñΪ¿Õ¡£±¾ÎĽ«Ì½ÌÖÔõÑùÅжÏmql¹¤¾ßÊÇ·ñΪ¿Õ¡£
ʲôÊÇMQL¹¤¾ß
MQL£¨Model Query Language£©¹¤¾ßÊÇThinkPHP5Öлù´¡Ä£×ÓÀàQueryµÄʵÀý£¬ËüÓÃÓÚ¹¹½¨Êý¾Ý¿âµÄÅÌÎÊÌõ¼þºÍ²Ù×÷¡£
ÔÚThinkPHP5ÖÐÿ¸öÄ£×Ó¶¼ÓÐÒ»¸öĬÈϵÄMQL¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÄ£×ӵľ²Ì¬ÒªÁì»ñµÃÕâ¸ö¹¤¾ß£¬È磺
$userModel = new \app\user\model\UserModel; $userModel->where('username', 'like', '%admin%')->select();
µÇ¼ºó¸´ÖÆ
ͬÑù¿ÉÒÔд³É£º
$userModel = \app\user\model\UserModel::where('username', 'like', '%admin%')->select();
µÇ¼ºó¸´ÖÆ
ÅжÏMQL¹¤¾ßÊÇ·ñΪ¿Õ
ÔÚ²Ù×÷Êý¾Ý¿âʱ£¬ÎÒÃÇÓÐʱ»áÓöµ½ÅÌÎÊЧ¹ûΪ¿ÕµÄÇéÐΣ¬ÕâʱÎÒÃÇÐèÒªÅжÏMQL¹¤¾ßÊÇ·ñΪ¿Õ¡£ÅжÏÒªÁìÓÐÒÔϼ¸ÖÖ£º
ͨ¹ýcount()ÒªÁìÅжÏ
MQL¹¤¾ßÌṩÁËcount()ÒªÁ죬ÓÃÓÚÅÌÎÊÇкÏÌõ¼þµÄ¼Í¼ÊýÄ¿¡£ÈôÊÇ·µ»ØµÄ¼Í¼ÊýĿΪ0£¬Ôò˵Ã÷MQL¹¤¾ßΪ¿Õ¡£
$userModel = \app\user\model\UserModel::where('username', 'like', '%notexist%'); if($userModel->count() == 0){ echo 'MQL¹¤¾ßΪ¿Õ'; }
µÇ¼ºó¸´ÖÆ
ͨ¹ýfind()ÒªÁìÅжÏ
MQL¹¤¾ßÌṩÁËfind()ÒªÁ죬ÓÃÓÚÅÌÎÊÇкÏÌõ¼þµÄµÚÒ»Ìõ¼Í¼¡£ÈôÊÇ·µ»ØµÄЧ¹ûΪnull£¬Ôò˵Ã÷MQL¹¤¾ßΪ¿Õ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
$userModel = \app\user\model\UserModel::where('username', 'like', '%notexist%')->find(); if(is_null($userModel)){ echo 'MQL¹¤¾ßΪ¿Õ'; }
µÇ¼ºó¸´ÖÆ
ͨ¹ýselect()ÒªÁìÅжÏ
MQL¹¤¾ßÌṩÁËselect()ÒªÁ죬ÓÃÓÚÅÌÎÊÇкÏÌõ¼þµÄËùÓмͼ¡£ÈôÊÇ·µ»ØµÄЧ¹ûΪ¿ÕÊý×飬Ôò˵Ã÷MQL¹¤¾ßΪ¿Õ¡£
$userModel = \app\user\model\UserModel::where('username', 'like', '%notexist%')->select(); if(empty($userModel)){ echo 'MQL¹¤¾ßΪ¿Õ'; }
µÇ¼ºó¸´ÖÆ
ͨ¹ýisEmpty()ÒªÁìÅжÏ
MQL¹¤¾ßÌṩÁËisEmpty()ÒªÁ죬ÓÃÓÚÅжÏMQL¹¤¾ßÊÇ·ñΪ¿Õ¡£ÈôÊÇ·µ»ØЧ¹ûΪtrue£¬Ôò˵Ã÷MQL¹¤¾ßΪ¿Õ¡£
$userModel = \app\user\model\UserModel::where('username', 'like', '%notexist%'); if($userModel->isEmpty()){ echo 'MQL¹¤¾ßΪ¿Õ'; }
µÇ¼ºó¸´ÖÆ
×ܽá
ÔÚʹÓÃThinkPHP5µÄMQL¹¤¾ßʱ£¬ÎÒÃÇÐèÒª¾³£ÅжÏMQL¹¤¾ßÊÇ·ñΪ¿Õ¡£±¾ÎÄÏÈÈÝÁËËÄÖÖÅжÏÒªÁ죬»®·ÖÊÇͨ¹ýcount()¡¢find()¡¢select()ºÍisEmpty()ÒªÁìÀ´¾ÙÐÐÅжϡ£Ñ¡ÔñÊʵ±µÄÅжÏÒªÁìÄܹ»Ê¹´úÂëÔ½·¢¾«Á·ÓÅÑÅ£¬Í¬Ê±Ò²Äܹ»Ìá¸ß´úÂëµÄÔËÐÐЧÂÊ¡£
ÒÔÉϾÍÊÇthinkphp5̽ÌÖÔõÑùÅжÏMQL¹¤¾ßÊÇ·ñΪ¿ÕµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡