Ïê½âthinkphpÖеÄÅÌÎÊÓï¾ä
thinkphp ÊÇÒ»¿î¿ªÔ´µÄ»ùÓÚphpµÄwebÓ¦Óÿò¼Ü£¬ËüÌṩÁËÐí¶àÀû±ã¿ª·¢µÄ¹¤¾ßÏ¢Õù¾ö¼Æ»®£¬°üÀ¨ÅÌÎÊÓï¾äµÄ¹¹½¨ºÍÖ´ÐС£±¾ÎĽ«ÏÈÈÝ thinkphp ÖеÄÅÌÎÊÓï¾ä¹¦Ð§£¬°üÀ¨ÅÌÎʹ¹½¨Æ÷ºÍÊý¾ÝÄ£×Ó£¬×ÊÖú¿ª·¢Õ߸üÓÐÓõؾÙÐÐÊý¾Ý¿â²Ù×÷¡£
Ò»¡¢ÅÌÎʹ¹½¨Æ÷
ThinkPHP µÄÅÌÎʹ¹½¨Æ÷ÊÇÒ»Ì×ÃæÏò¹¤¾ßµÄ SQL Óï¾ä¹¹½¨Æ÷£¬ÓÃÓÚ¿ìËÙ¹¹½¨ÖØ´ó SQL Óï¾ä¡£ÔÚÅÌÎʹ¹½¨Æ÷ÖУ¬¿ÉÒÔͨ¹ýʹÓöàÖÖÒªÁìÀ´¹¹½¨ SELECT¡¢UPDATE¡¢INSERT ºÍ DELETE µÈÀàÐ굀 SQL Óï¾ä¡£
SELECT ÅÌÎÊÓï¾ä
ʹÓà SELECT ÅÌÎÊÓï¾ä¿ÉÒÔ»ñÈ¡Êý¾Ý¿âÖеÄÊý¾Ý¡£¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷µÄ select() ÒªÁìÀ´¹¹½¨ SELECT ÅÌÎÊÓï¾ä£º
// ÅÌÎÊËùÓÐÓû§Êý¾Ý Db::name('user')->select(); // ÅÌÎÊ id Ϊ 1 µÄÓû§Êý¾Ý Db::name('user')->where('id', 1)->find(); // ÅÌÎÊÄêËê´óÓÚ 18 ËêµÄÓû§Êý¾Ý Db::name('user')->where('age', '>', 18)->select();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬Db::name(‘user’) ÌåÏÖÒªÅÌÎʵÄÊý¾Ý±í¡£select() ÒªÁìÌåÏÖÅÌÎÊËùÓÐÊý¾Ý£»find() ÒªÁìÌåÏÖÅÌÎʵ¥Ìõ¼Í¼¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
UPDATE ÅÌÎÊÓï¾ä
ʹÓà UPDATE ÅÌÎÊÓï¾ä¿ÉÒÔ¸üÐÂÊý¾Ý¿âÖеÄÊý¾Ý¡£¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷µÄ update() ÒªÁìÀ´¹¹½¨ UPDATE ÅÌÎÊÓï¾ä£º
// ¸üРid Ϊ 1 µÄÓû§Êý¾Ý Db::name('user')->where('id', 1)->update(['name' => 'Tom']); // ½«ËùÓÐÓû§µÄ½ÇÉ«¶¼¸üÐÂΪ 2 Db::name('user')->update(['role_id' => 2]);
µÇ¼ºó¸´ÖÆ
ÔÚ update() ÒªÁìÖУ¬µÚÒ»¸ö²ÎÊýÌåÏÖÒª¸üеļͼµÄÌõ¼þ£¬µÚ¶þ¸ö²ÎÊýΪ¸üеÄÊý¾ÝÄÚÈÝ¡£
INSERT ÅÌÎÊÓï¾ä
ʹÓà INSERT ÅÌÎÊÓï¾ä¿ÉÒÔÏòÊý¾Ý¿âÖвåÈëÐÂÊý¾Ý¡£¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷µÄ insert() ÒªÁìÀ´¹¹½¨ INSERT ÅÌÎÊÓï¾ä£º
// Ïò user ±íÖвåÈëÒ»ÌõÐÂÊý¾Ý Db::name('user')->insert(['name' => 'Jack', 'age' => 20, 'role_id' => 1]);
µÇ¼ºó¸´ÖÆ
ÔÚ insert() ÒªÁìÖУ¬²ÎÊýΪҪ²åÈëµÄÐÂÊý¾ÝÄÚÈÝ¡£
DELETE ÅÌÎÊÓï¾ä
ʹÓà DELETE ÅÌÎÊÓï¾ä¿ÉÒÔɾ³ýÊý¾Ý¿âÖеÄÊý¾Ý¡£¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷µÄ delete() ÒªÁìÀ´¹¹½¨ DELETE ÅÌÎÊÓï¾ä£º
// ɾ³ý id Ϊ 1 µÄÊý¾Ý Db::name('user')->where('id', 1)->delete(); // ɾ³ýËùÓнÇɫΪ 3 µÄÓû§Êý¾Ý Db::name('user')->where('role_id', 3)->delete();
µÇ¼ºó¸´ÖÆ
ÔÚ delete() ÒªÁìÖУ¬²ÎÊýΪҪɾ³ýµÄ¼Í¼µÄÌõ¼þ¡£
¶þ¡¢Êý¾ÝÄ£×Ó
³ýÁËÅÌÎʹ¹½¨Æ÷Ö®Í⣬ThinkPHP »¹ÌṩÁËÒ»Ì×»ùÓÚÊý¾ÝÄ£×ÓµÄÊý¾Ý¿â²Ù×÷·½·¨£¬¸Ã·½·¨¿ÉÒÔ½«Êý¾Ý±íÓ³Éä³ÉΪһ¸öÀ࣬²¢ÇÒ¿ÉÒÔƾ֤ÀàÊôÐÔµÄת±ä×Ô¶¯´¦ÀíÊý¾Ý¿â¼Í¼µÄ CRUD¡£
½ç˵Êý¾ÝÄ£×Ó
¿ÉÒÔʹÓÃÒÔÏ´úÂë½ç˵һ¸öÊý¾ÝÄ£×ÓÀࣺ
namespace app\common\model; use think\Model; class User extends Model { // Êý¾Ý±íÃû protected $table = 'user'; // ×Ô¶¯Ð´Èëʱ¼ä´Á protected $autoWriteTimestamp = true; // Ä£×Ó¹ØÁª£ºÓû§½ÇÉ« public function role() { return $this->belongsTo(Role::class, 'role_id'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇͨ¹ý¼ÌÐø think\ModelÀàÀ´½ç˵ÁËÒ»¸öÓû§Êý¾ÝÄ£×ÓÀà¡£ÆäÖÐ $table ÊôÐÔÌåÏÖÒªÓ³ÉäµÄÊý¾Ý±íÃû£¬$autoWriteTimestamp ÊôÐÔÌåÏÖÊÇ·ñ×Ô¶¯Ð´Èëʱ¼ä´Á¡£
ÅÌÎÊÊý¾Ý
ÎÒÃÇ¿ÉÒÔʹÓÃÊý¾ÝÄ£× find()¡¢select()¡¢where() µÈÒªÁìÀ´ÅÌÎÊÊý¾Ý¿â£º
// ÅÌÎÊ id Ϊ 1 µÄÓû§Êý¾Ý $user = User::find(1); // ÅÌÎÊÓû§±íÖÐËùÓÐÊý¾Ý $users = User::select(); // ÅÌÎÊÄêËê´óÓÚ 18 ËêµÄÓû§Êý¾Ý $users = User::where('age', '>', 18)->select();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËÊý¾ÝÄ£×ÓÀàµÄ¾²Ì¬ÒªÁìÀ´¾ÙÐÐÊý¾Ý¿âÅÌÎÊ¡£
¸üкͲåÈëÊý¾Ý
ÎÒÃÇ¿ÉÒÔʹÓÃÊý¾ÝÄ£× save() ÒªÁìÀ´¸üкͲåÈëÊý¾Ý£º
// ¸üРid Ϊ 1 µÄÓû§Êý¾Ý $user = User::find(1); $user->name = 'Tom'; $user->save(); // Ïò user ±íÖвåÈëÒ»ÌõÐÂÊý¾Ý $user = new User; $user->name = 'Jack'; $user->age = 20; $user->role_id = 1; $user->save();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÊý¾ÝÄ£×Ó¹¤¾ßµÄÊôÐÔÀ´ÉèÖÃÒª¸üлò²åÈëµÄÊý¾Ý£¬È»ºóŲÓà save() ÒªÁìÌá½»µ½Êý¾Ý¿âÖС£
ɾ³ýÊý¾Ý
ÎÒÃÇ¿ÉÒÔʹÓÃÊý¾ÝÄ£× delete() ÒªÁìÀ´É¾³ýÊý¾Ý£º
// ɾ³ý id Ϊ 1 µÄÓû§Êý¾Ý $user = User::find(1); $user->delete(); // ɾ³ýËùÓнÇɫΪ 3 µÄÓû§Êý¾Ý User::where('role_id', 3)->delete();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËÊý¾ÝÄ£×Ó¹¤¾ßµÄ delete() ÒªÁìɾ³ýÖ¸¶¨¼Í¼£¬Ò²¿ÉÒÔʹÓþ²Ì¬ÒªÁìµÄ where() ÒªÁìÀ´Ñ¡ÕªÒªÉ¾³ýµÄ¼Í¼£¬È»ºóŲÓà delete() ÒªÁìɾ³ý¡£
×ܽá
ÒÔÉϾÍÊÇʹÓà ThinkPHP ¾ÙÐÐÅÌÎÊÓï¾ä¹¹½¨µÄÒªÁ죬ÆäÖÐÅÌÎʹ¹½¨Æ÷ÌṩÁ˶àÖÖÒªÁìÀ´¹¹½¨ SELECT¡¢UPDATE¡¢INSERT ºÍ DELETE µÈÀàÐ굀 SQL Óï¾ä£¬Êý¾ÝÄ£×ÓÔòÌṩÁËÃæÏò¹¤¾ßµÄ·½·¨À´²Ù×÷Êý¾Ý¿â¡£ÎÞÂÛÊÇʹÓÃÅÌÎʹ¹½¨Æ÷ÕÕ¾ÉÊý¾ÝÄ£×Ó£¬ÎÒÃǶ¼¿ÉÒÔ¿ìËٵع¹½¨³öÖØ´óµÄ SQL ÅÌÎÊÓï¾ä£¬Àû±ã¿ª·¢Õß¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£
ÒÔÉϾÍÊÇÏê½âthinkphpÖеÄÅÌÎÊÓï¾äµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡