thinkphpÔõÑùʹÓÃORM¾ÙÐÐÊý¾Ý¿â²Ù×÷
ThinkPHP ÊÇÒ»¿î»ùÓÚ PHP µÄ Web ¿ª·¢¿ò¼Ü£¬Ëü¾ßÓпìËÙ¡¢ÇáÓ¯¡¢Çå¾²µÄÌص㣬²¢ÇÒ±»ÆÕ±éµØʹÓ᣸ÃϵͳµÄORMÓ³Éä²ãÊÇ×îÒªº¦µÄ£¬ËüÄܹ»ÇáËÉ´¦ÀíÊý¾Ý¿â²Ù×÷¶øÎÞÐè±àдSQLÓï¾ä¡£¿ÉÊÇÓÐʱ¼äÔÚ¿ª·¢Àú³ÌÖл᷺ÆðÐèÒªÅŲé SQL Óï¾äµÄÇéÐΣ¬Õâʱ¼äÎÒÃǾÍÐèÒªÈà ThinkPHP Ö´ÐÐ SQL¡£
ÔÚ ThinkPHP ÖУ¬Ê¹Óà ORM ¾ÙÐÐÊý¾Ý¿â²Ù×÷ÓÐÁ½ÖÖÇéÐΣ¬Ò»ÖÖÊÇͨ¹ýÄ£×ÓÀ´²Ù×÷£¬ÁíÒ»ÖÖÊÇͨ¹ý Query ÀàÀ´²Ù×÷¡£
1¡¢Ä£×Ó²Ù×÷
Ä£×Ó²Ù×÷ÊÇʹÓà ORM À´¾ÙÐÐÊý¾Ý¿â²Ù×÷µÄÒ»ÖÖ·½·¨£¬Í¨¹ýÄ£×Ó²Ù×÷¿ÉÒÔºÜÀû±ãµØ¾ÙÐÐÔöɾ¸Ä²é²Ù×÷£¬²¢ÇÒ²»ÐèÒª±àд SQL Óï¾ä¡£ÓÐʱÐèҪʹÓô¿´âµÄ SQL ÅÌÎÊ£¬ÀýÈç²éÕÒij¸öÁеÄ×î´óÖµ¡¢×îСֵµÈ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚÄ£×ÓÅÌÎʵĻù´¡ÉÏ£¬ThinkPHP ÌṩÁËÒ»¸ö Db À࣬ͨ¹ýËü¿ÉÒÔÖ´ÐÐÔÉú SQL£¬ÈçÏÂËùʾ£º
use think\facade\Db; // Ö´ÐÐÔÉú SQL Db::query('SELECT * FROM `user` WHERE `id` = 1');
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ý Db::query() ÒªÁìÀ´Ö´ÐÐÔÉú SQL Óï¾ä¡£Õâ¸öÒªÌå»á·µ»ØÒ»¸öÊý×飬°üÀ¨ÅÌÎÊЧ¹û¡£
2¡¢Query Àà²Ù×÷
Query ÀàÊÇ ThinkPHP ÖÐÌṩµÄÒ»¸öÁ´Ê½²Ù×÷À࣬Ëü¿ÉÒÔÀû±ãµØ¹¹½¨ SQL Óï¾ä£¬²¢ÇÒÖ§³ÖÔÉú SQL ÅÌÎÊ¡£
ʹÓà Query Àà²Ù×÷ SQL µÄ»ù±¾Ó÷¨ÈçÏ£º
use think\facade\Db; $query = Db::table('user'); $data = $query->where('id', 1) ->field('id, name') ->select(); // Ö´ÐÐÔÉú SQL $sql = $query->fetchSql(true)->find();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏÈͨ¹ý Db::table() ÒªÁìÀ´»ñÈ¡Ò»¸ö Query À๤¾ß£¬È»ºóÔÚ Query À๤¾ßÉϾÙÐÐÁ´Ê½²Ù×÷£¬Ê¹Óà where() ÒªÁìÌí¼ÓÅÌÎÊÌõ¼þ£¬Ê¹Óà field() ÒªÁìÑ¡ÕªÒªÅÌÎʵÄ×ֶΣ¬Ê¹Óà select() ÒªÁìÅÌÎÊЧ¹û¡£
ÈôÊÇÐèÒªÖ´ÐÐÔÉú SQL Óï¾ä£¬Ö»ÐèÒªÔÚ Query À๤¾ßŲÓà fetchSql(true) ÒªÁì¼´¿É£¬¸ÃÒªÁ콫·µ»ØÌìÉúµÄ SQL Óï¾ä¡£
ÒÔÉϾÍÊÇthinkphpÔõÑùʹÓÃORM¾ÙÐÐÊý¾Ý¿â²Ù×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡