thinkphpÔõÑùÐÞ¸ÄÊý¾Ý¿â
Ò»¡¢ThinkPHPÊý¾Ý¿â²Ù×÷
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýËüÌṩµÄÊý¾Ý¿â²Ù×÷ÀàÀ´¾ÙÐжÔÊý¾Ý¿âµÄ²Ù×÷¡£³£ÓõÄÊý¾Ý¿â²Ù×÷ÀàÓУº
DbÀà
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃDbÀàÀ´¾ÙÐжÔÊý¾Ý¿âµÄÔö¡¢É¾¡¢¸Ä¡¢²éµÈ²Ù×÷¡£ÆäʹÓÃʾÀýÈçÏ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
<?php use think\Db; // ÅÌÎÊÊý¾Ý $list = Db::table('user')->where('id', 1)->find(); // ÐÂÔöÊý¾Ý $data['name'] = 'test'; $data['age'] = 20; Db::table('user')->insert($data); // ¸üÐÂÊý¾Ý $where['id'] = 1; $data['name'] = 'test'; $data['age'] = 30; Db::table('user')->where($where)->update($data); // ɾ³ýÊý¾Ý $where['id'] = 1; Db::table('user')->where($where)->delete();
µÇ¼ºó¸´ÖÆ
ModelÀà
ÔÚThinkPHPÖУ¬ModelÀà¼ÌÐø×ÔDbÀ࣬Òò´ËModelÀà¿ÉÒÔʹÓÃDbÀàµÄËùÓÐÒªÁ죬ͬʱ»¹ÌṩÁËһЩԽ·¢Àû±ãµÄÒªÁì¡£ÆäʹÓÃʾÀýÈçÏ£º
<?php namespace app\index\model; use think\Model; class User extends Model { // ÅÌÎÊÊý¾Ý public function getUserById($id) { return $this->where('id', $id)->find(); } // ¸üÐÂÊý¾Ý public function updateUser($id, $name) { return $this->save(['name' => $name], ['id' => $id]); } }
µÇ¼ºó¸´ÖÆ
¶þ¡¢ThinkPHPÔõÑùÐÞ¸ÄÊý¾Ý¿â
ÔÚʹÓÃThinkPHPÐÞ¸ÄÊý¾Ý¿âʱ£¬Í¨³£»á¾ÓÉÒÔϼ¸¸ö°ì·¨£º
½¨ÉèÄ£×Ó
ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÄ£×Ó£¬¸ÃÄ£×Ó¶ÔÓ¦×ÅÎÒÃÇÐèÒª»á¼ûµÄÊý¾Ý¿â±í¡£ÓÉÓÚThinkPHP½ÓÄÉÁËMVCÉè¼Æģʽ£¬Òò´ËÎÒÃÇÔÚ½¨ÉèÄ£×ÓʱÐèÒª¼ÌÐø×ÔThinkPHPµÄModelÀà¡£ÏÂÃæÊÇÒ»¸ö½¨ÉèUserÄ£×ÓµÄʾÀý£º
<?php namespace app\index\model; use think\Model; class User extends Model { protected $table = 'user'; protected $pk = 'id'; }
µÇ¼ºó¸´ÖÆ
ÔÚ½¨ÉèUserÄ£×Óʱ£¬ÎÒÃÇÖ¸¶¨Á˸ÃÄ£×Ó¶ÔÓ¦µÄÊý¾Ý¿â±íÊÇuser±í£¬²¢ÇҸñíµÄÖ÷¼üÊÇid¡£
ʵÀý»¯Ä£×Ó
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪʹÓÃÏÈÇ°½¨ÉèµÄUserÄ£×Ó¾ÙÐÐʵÀý»¯£¬²¢Ê¹ÓøÃÄ£×Ó²Ù×÷Êý¾Ý¿â¡£ÏÂÃæÊÇʵÀý»¯UserÄ£×ÓµÄʾÀý£º
<?php $userModel = new \app\index\model\User();
µÇ¼ºó¸´ÖÆ
ÐÞ¸ÄÊý¾Ý
Ò»µ©½¨ÉèÁËUserÄ£×ÓµÄʵÀý£¬¾Í¿ÉÒÔʹÓøÃÄ£×ÓËùÌṩµÄº¯Êý¶ÔÊý¾Ý¿â¾ÙÐÐÐ޸ġ£ÏÂÃæÊÇÒ»¸öʹÓÃUserÄ£×ÓÐÞ¸ÄÊý¾ÝµÄʾÀý£º
<?php $userModel = new \app\index\model\User(); // ¸üÐÂÊý¾Ý $where['id'] = 1; $data['name'] = 'test'; $data['age'] = 30; $userModel->where($where)->update($data);
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏʾÀýÖУ¬ÎÒÃÇʹÓÃ$UserModelµÄupdate()ÒªÁì¶ÔUser±íÖÐidΪ1µÄÊý¾Ý¾ÙÐÐÁËÐ޸ģ¬½«¸ÃÐÐÊý¾ÝµÄname×Ö¶ÎÐÞ¸ÄΪtest£¬age×Ö¶ÎÐÞ¸ÄΪ30¡£
ÒÔÉϾÍÊÇthinkphpÔõÑùÐÞ¸ÄÊý¾Ý¿âµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡