thinkphpÔõÑù¾ÙÐÐÄ£ºýÅÌÎÊ
Ê×ÏÈ£¬ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýDbÀàÀ´²Ù×÷Êý¾Ý¿â¡£ÎÒÃÇ¿ÉÒÔʹÓÃDbÀàµÄtableÒªÁìÀ´Ö¸¶¨Òª²Ù×÷ÄĸöÊý¾Ý±í£¬È»ºóŲÓÃÏìÓ¦µÄÅÌÎÊÒªÁì¾ÙÐвÙ×÷¡£ÔÚ¾ÙÐÐÄ£ºýÅÌÎÊʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃlikeÒªÁì¡£ÐèҪģºýÆ¥ÅäµÄ×Ö¶ÎÃû³Æ×÷Ϊ¸ÃÒªÁìµÄµÚÒ»¸ö²ÎÊý£¬ÐèҪƥÅäµÄ×Ö·û´®×÷ΪµÚ¶þ¸ö²ÎÊý¡£ÀýÈ磬ÎÒÃÇÒªÅÌÎÊÃû×Ö°üÀ¨¡°ÕÅ¡±µÄËùÓÐÓû§£¬¿ÉÒÔÕâÑùд´úÂ룺
Db::table('user')->where('name', 'like', '%ÕÅ%')->select();
µÇ¼ºó¸´ÖÆ
ÕâÌõÓï¾äÖУ¬whereÒªÁìµÄµÚÒ»¸ö²ÎÊýÊÇÐèÒªÅÌÎʵÄÌõ¼þ£¬ÆäÖÐ’name’ÊÇÊý¾Ý±íÖеÄ×Ö¶ÎÃû³Æ£¬µÚ¶þ¸ö²ÎÊý’like’ÌåÏÖÎÒÃÇÒª¾ÙÐÐÄ£ºýÆ¥Å䣬µÚÈý¸ö²ÎÊý’%ÕÅ%’ÊÇÐèҪƥÅäµÄ×Ö·û´®¡£Ê¹ÓÃ%ͨÅä·û£¬¿ÉÒÔÆ¥Åä°üÀ¨í§ÒâÊýÄ¿×Ö·ûµÄ×Ö·û´®£¬Òò´Ë¿ÉÒÔÕÒµ½ËùÓÐÓû§ÃûÖаüÀ¨¡°ÕÅ¡±µÄÓû§¡£
³ýÁËʹÓÃDbÀàÌṩµÄÅÌÎÊÒªÁìÍ⣬ThinkPHP»¹ÌṩÁËһЩԽ·¢¸ß¼¶µÄÅÌÎÊ·½·¨£¬ÈçÄ£×ÓÅÌÎʺ͹ØÁªÅÌÎʵȵȡ£½ç˵ģ×ÓÀàÄܹ»¼ò»¯ÅÌÎÊ£¬ÓÉÓÚÄ£×ÓÅÌÎÊÊÇͨ¹ýʹÓýç˵µÄÄ£×ÓÀàʵÏֵġ£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚUserÄ£×ÓÖнç˵һ¸ösearchByNameÒªÁ죬ÓÃÓÚÅÌÎÊÃû×ÖÖаüÀ¨Ö¸¶¨×Ö·û´®µÄËùÓÐÓû§£º
class User extends Model { public function searchByName($keyword) { return $this->where('name', 'like', '%' . $keyword . '%')->select(); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÒªÁìÖУ¬$this´ú±íÄ¿½ñUserÄ£×ӵŤ¾ß£¬whereÒªÁìºÍÉÏÃæÏÈÈݵÄÒ»Ñù£¬Ö»Êǽ«DbÀà»»³ÉÁË$this¡£ÎÒÃÇ¿ÉÒÔ·â×°Õâ¸öÅÌÎÊÒªÁ죬ÒÔ±ãÔÚÓ¦ÓóÌÐòµÄÈκεط½¶¼Äܹ»Öظ´Ê¹ÓÃËü
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
class User extends Model { public static function searchByName($keyword) { return self::where('name', 'like', '%' . $keyword . '%')->select(); } }
µÇ¼ºó¸´ÖÆ
ÕâÀïÎÒÃǽ«searchByNameÒªÁì¸ÄΪ¾²Ì¬ÒªÁ죬²¢½«$this»»³ÉÁËself£¬ÕâÑù¾Í¿ÉÒÔÔÚÈκεط½Ö±½ÓŲÓÃUser::searchByName()ÒªÁì¾ÙÐÐÅÌÎÊÁË¡£
¹ØÁªÅÌÎÊÊÇͨ¹ýʹÓÃJOINÓï¾ä½«¶à¸öÏà¹ØµÄÊý¾Ý±íÅþÁ¬ÆðÀ´¾ÙÐÐÅÌÎʵÄÒªÁì¡£ThinkPHPÖеĹØÁªÅÌÎÊÊÇͨ¹ý½ç˵ģ×Ó¹ØÁªÀ´ÊµÏֵģ¬ÏêϸʵÏÖ·½·¨Áè¼ÝÁ˱¾ÎĵÄÁìÓò£¬¿ÉÊÇÐèҪעÖصÄÊÇ£¬µ±¾ÙÐÐÄ£ºýÅÌÎÊʱ£¬JOINÓï¾ä»á½µµÍÅÌÎÊЧÂÊ£¬Õâʱ¿ÉÒÔ˼Á¿ÏȾÙÐÐÄ£ºýÅÌÎÊ£¬ÔÙ¾ÙÐйØÁªÅÌÎÊ£¬»òÕßʹÓûº´æµÈ·½·¨À´Ìá¸ßÅÌÎÊËÙÂÊ¡£
ÒÔÉϾÍÊÇthinkphpÔõÑù¾ÙÐÐÄ£ºýÅÌÎʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡