thinkphp6µãÄ£×ÓÓÐʲô×÷ÓÃ
thinkphp6ÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶àÇ¿Ê¢µÄ¹¦Ð§ºÍ¹¤¾ß£¬Ê¹µÃ¿ª·¢Õß¿ÉÒÔ¿ìËٵرàд¸ßЧµÄwebÓ¦ÓóÌÐò¡£ÆäÖУ¬µãÄ£×Ó£¨dot model£©ÊÇÆäÖÐÒ»¸öºÜÊÇÖ÷ÒªµÄ¿´·¨ºÍ¹¤¾ßÖ®Ò»¡£
µãÄ£×ÓÊÇÒ»ÖÖÇáÁ¿¼¶µÄÄ£×Ó½ç˵·½·¨£¬Ëü½ÓÄÉÁËÀàËÆÓÚÁ´Ê½»á¼ûµÄ·½·¨À´½ç˵ģ×ÓµÄ×ֶκ͹Øϵ¡£ÔÚThinkPHP6ÖУ¬µãÄ£×ÓÊÇͨ¹ý¼ÌÐøThinkModelÀàÀ´ÊµÏֵġ£
µãÄ£×ÓµÄÖ÷Òª×÷ÓÃÊǼò»¯Ä£×ӵĽç˵ºÍ²Ù×÷¡£¹Å°åµÄÄ£×Ó½ç˵·½·¨ÐèÒªÊÖ¶¯½ç˵һЩ»ù±¾ÒªÁ죬Èçcreate¡¢update¡¢findµÈ£¬ÒÔ¼°Ò»Ð©ÅÌÎÊÌõ¼þºÍ¹ØÁª¹Øϵ¡£¶øµãÄ£×ÓÔòʹÓÃÔ½·¢¾«Á·Ã÷ÎúµÄ·½·¨À´½ç˵ÕâЩ¹¦Ð§ºÍÄÚÈÝ£¬Ê¹ÓÃÆðÀ´Ô½·¢Àû±ãºÍÈÝÒס£
ÏêϸÀ´Ëµ£¬µãÄ£×ÓµÄ×÷ÓðüÀ¨ÒÔϼ¸¸ö·½Ã棺
¿ìËÙ½ç˵ģ×Ó×Ö¶Î
µãÄ£×Ó¿ÉÒÔͨ¹ý×Ö¶ÎÃûÀ´¼òÆÓÃ÷ÎúµØ½ç˵ģ×ÓµÄ×ֶΣ¬ÀýÈ磺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
class UserModel extends Model { protected $field = [ 'id', 'name', 'email', 'password', ]; }
µÇ¼ºó¸´ÖÆ
ÕâÑù¾Í½ç˵ÁËÒ»¸ö°üÀ¨id¡¢name¡¢emailºÍpasswordËĸö×ֶεÄUserModelÄ£×Ó¡£
Ö§³ÖÁ´Ê½²Ù×÷
µãÄ£×ÓÖ§³ÖÁ´Ê½²Ù×÷£¬ÕâʹµÃÔÚ²Ù×÷Ä£×Óʱ¿ÉÒÔÔ½·¢Á÷ͨºÍ¼òÆÓ¡£ÀýÈ磬¿ÉÒÔʹÓÃÁ¬¹á²Ù×÷À´½ç˵ÅÌÎÊÌõ¼þºÍ¹ØÁª¹Øϵ£º
class UserModel extends Model { public function posts() { return $this->hasMany(PostModel::class, 'user_id'); } } $users = UserModel::where('name', 'like', '%Tom%')->with('posts')->select();
µÇ¼ºó¸´ÖÆ
ÕâÑù¾Í½ç˵ÁËÒ»¸öUserModelµÄposts¹ØÁª¹Øϵ£¬²¢ÇÒÔÚÅÌÎÊÓû§Êý¾Ýʱͨ¹ýwithÒªÁìÒ»²¢ÅÌÎÊÁ˹ØÁªµÄpostÊý¾Ý¡£ÕâÑù¿ÉÒÔ×èֹʹÓÃÌØÁíÍâÅÌÎÊÀ´»ñÈ¡¹ØÁªÊý¾Ý£¬Ìá¸ßÅÌÎÊЧÂÊ¡£
Ö§³Ö×Ô¶¯ÑéÖ¤
µãÄ£×ÓÖ§³Ö×Ô¶¯ÑéÖ¤£¬¿ÉÒÔ×èÖ¹·±ËöµÄÊÖ¶¯ÑéÖ¤ºÍÅжϡ£ÀýÈ磬ͨ¹ý½ç˵ÑéÖ¤¹æÔòÀ´È·±£Ä£×ÓÊý¾ÝµÄ׼ȷÐÔ£º
class UserModel extends Model { protected $rule = [ 'name' => 'require', 'email' => 'email', 'password' => 'require|length:6,20', ]; }
µÇ¼ºó¸´ÖÆ
ÕâÑù¾Í½ç˵ÁËÒ»¸öUserModelÄ£×ÓµÄÑéÖ¤¹æÔò£¬È·±£ÁËÐÅÏ¢µÄ׼ȷÐÔ£¬Ìá¸ßÁËÊý¾ÝµÄÇå¾²ÐÔ¡£
¼ò»¯CRUD²Ù×÷
µãÄ£×Ó¿ÉÒÔʹÓþ«Á·Ã÷ÎúµÄ·½·¨À´¾ÙÐÐCRUD²Ù×÷¡£ÀýÈ磺
$user = UserModel::find(1); $user->name = 'Tom'; $user->save(); $user = UserModel::create([ 'name' => 'John', 'email' => 'john@example.com', 'password' => '123456', ]); $user = UserModel::destroy([1,2,3]);
µÇ¼ºó¸´ÖÆ
ÕâÑù¾Í½ç˵ÁËÄ£×ÓµÄÔöɾ¸Ä²é²Ù×÷£¬Ê¹ÓÃÆðÀ´Ô½·¢Àû±ãºÍ¿ì½Ý¡£
×ÜÖ®£¬µãÄ£×ÓÊÇThinkPHP6ÖкÜÊÇÖ÷ÒªºÍÊÊÓõĿ´·¨ºÍ¹¤¾ßÖ®Ò»£¬Ëü¿ÉÒÔ¼«´óµØ¼ò»¯Ä£×ӵĽç˵ºÍ²Ù×÷£¬Ê¹µÃ¿ª·¢Õß¿ÉÒÔÔ½·¢Ð§Âʵرàд¸ßЧµÄWebÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇthinkphp6µãÄ£×ÓÓÐʲô×÷ÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡