ÁÄÁÄThinkPHP 5.0 ÖÐÄ£×ÓµÄʹÓÃÒªÁì
thinkphp 5.0 ÊÇÏÖÔÚÔÚº£ÄÚʹÓÃÆÕ±éµÄ php ¿ª·¢¿ò¼ÜÖ®Ò»£¬²»µ«ÔÚ½¹µã´úÂëÉÏ×öÁË´ó×ÚµÄÓÅ»¯ºÍˢУ¬»¹Ìí¼ÓÁËÐí¶àÐµĹ¦Ð§ºÍÌØÕ÷£¬ÆäÖÐÄ£×Ó£¨model£©Ò²»ñµÃÁ˺ܴóµÄÉý¼¶¡£±¾ÎĽ«ÏêϸÏÈÈÝ thinkphp 5.0 ÖÐÄ£×ÓµÄʹÓÃÒªÁì¡£
Ò»¡¢Ê²Ã´ÊÇÄ£×Ó
Ä£×Ó¼òÆÓÀ´Ëµ¾ÍÊÇÒ»¸öÊý¾ÝµÄ²Ù×÷À࣬ÓÃÓÚ¶ÔÊý¾Ý¿â²Ù×÷¡£ÔÚ ThinkPHP ÖУ¬Ä£×Ó¶ÔÊý¾Ý±í¾ÙÐÐÁË·â×°£¬¿ÉÒÔʵÏÖÀû±ã¿ì½ÝµÄ¶ÔÊý¾Ý±í¾ÙÐвÙ×÷¡£ÔÚ½¨ÉèÒ»¸öÄ£×Óʱ£¬Ö»ÐèÒª¼ÌÐø Think\Model ¼´¿É£¬¶ø²»±ØÔÙд´ó×ÚµÄÅÌÎÊºÍ SQL Óï¾ä¡£
¶þ¡¢½¨ÉèÒ»¸ö¼òÆÓµÄÄ£×Ó
Ê×ÏÈÔÚ ThinkPHP 5.0 Öн¨ÉèÒ»¸öÄ£×Ó
ÔÚ ThinkPHP 5.0 ÖУ¬½¨ÉèÒ»¸öÄ£×ÓºÜÊǼòÆÓ£¬Ö»ÐèÒªÔÚ application Ŀ¼Ï£¬Ð½¨Ò»¸ö model Ŀ¼£¬ÔÙÔÚ model Ŀ¼ÏÂн¨Ò»¸öÃûΪ User.php µÄÎļþ£¬´úÂëÈçÏ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
<?php namespace app\model; use think\Model; class User extends Model { }
µÇ¼ºó¸´ÖÆ
ÅþÁ¬Êý¾Ý¿â
ThinkPHP 5.0 ÖÐĬÈϽÓÄÉ PDO ·½·¨ÅþÁ¬Êý¾Ý¿â£¬Êý¾Ý¿âÅþÁ¬ÐÅÏ¢ÔÚÓ¦ÓÃĿ¼Ï嵀 database.php ÎļþÖоÙÐÐÉèÖá£ÅþÁ¬Àֳɺ󣬾ͿÉÒÔÔÚÄ£×ÓÖоÙÐÐÏìÓ¦µÄ²Ù×÷ÁË¡£
Ä£×ӵĻù±¾ CRUD ²Ù×÷
ÔÚ ThinkPHP 5.0 ÖУ¬Ä£×ӵĻù±¾ CRUD ²Ù×÷¶¼ÒѾ·â×°ºÃ£¬¿ÉÒÔÖ±½ÓŲÓá£ÒÔ User Ä£×ÓΪÀý£¬ÑÝʾһÏÂ×î³£¼ûµÄ CRUD ²Ù×÷£º
£¨1£©²åÈëÊý¾Ý
$user = new User(); $user->name = 'Tom'; $user->age = 20; $user->save();
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊDzåÈëÊý¾ÝµÄ×î³£¼û·½·¨£¬ÊµÀý»¯Ò»¸ö User ¹¤¾ß£¬È»ºóͨ¹ýÊôÐԵķ½·¨¸ø¹¤¾ß¸³Öµ£¬×îºóŲÓà save() ÒªÁ콫Êý¾ÝÉúÑĵ½Êý¾Ý¿âÖС£
£¨2£©É¾³ýÊý¾Ý
User::destroy(1);
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄ 1 ÊÇҪɾ³ýµÄÊý¾ÝµÄ ID£¬ÈôÊÇҪɾ³ý¶àÌõÊý¾Ý£¬¿ÉÒÔת´ïÒ»¸öÊý×é¡£»¹¿ÉÒÔʹÓà where ÒªÁì¾ÙÐÐÌõ¼þɾ³ý¡£
£¨3£©ÅÌÎÊÊý¾Ý
// ÅÌÎÊËùÓÐÊý¾Ý $users = User::all(); // ƾ֤Ìõ¼þÅÌÎʵ¥ÌõÊý¾Ý $user = User::where('name', 'Tom')->find(); // ƾ֤Ìõ¼þÅÌÎʶàÌõÊý¾Ý $users = User::where('age', '>', 18)->select();
µÇ¼ºó¸´ÖÆ
£¨4£©¸üÐÂÊý¾Ý
$user = User::get(1); $user->name = 'Jack'; $user->save();
µÇ¼ºó¸´ÖÆ
¼´ÏÈÅÌÎʳöÒªÐ޸ĵÄÊý¾Ý£¬ÐÞ¸ÄÊý¾Ýºóͨ¹ý save() ÒªÁìÉúÑĵ½Êý¾Ý¿âÖС£
Èý¡¢Ä£×Ó¹ØÁª²Ù×÷
ÔÚÏÖʵµÄ¿ª·¢ÖУ¬¾³£ÐèÒª¶Ô¶à¸öÊý¾Ý±í¾ÙÐÐÖØ´óµÄÍŽáÅÌÎʺ͹ØÁª²Ù×÷¡£ThinkPHP 5.0 Ä£×ÓÌṩÁ˸»ºñµÄ¹ØÁª²Ù×÷£¬Äܹ»¿ìËÙ½â¾ö±íÖ®¼äµÄ¹ØÁªÎÊÌâ¡£
Ò»¶ÔÒ»¹ØÁª
ÔÚ ThinkPHP 5.0 ÖУ¬Ò»¶ÔÒ»¹ØÁªÓÐÈýÖÖ·½·¨£º
£¨1£©¹ØÁªÄ£×ÓµÄÊôÐÔ
class User extends Model { public function profile() { return $this->hasOne('Profile'); } } class Profile extends Model { public function user() { return $this->belongsTo('User'); } } $user = User::get(1); $profile = $user->profile;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý hasOne() ÒªÁ콫 User Ä£×ÓÓë Profile Ä£×Ó¹ØÁªÆðÀ´£¬È»ºóŲÓà $user->profile ÊôÐÔ»ñÈ¡¹ØÁªÊý¾Ý¡£
£¨2£©¹ØÁªÅÌÎÊ
$user = User::with('profile')->select(); $profile = $user->profile;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý with() ÒªÁìÖ±½Ó¾ÙÐйØÁªÅÌÎÊ£¬È»ºóŲÓà $user->profile ÊôÐÔ»ñÈ¡¹ØÁªÊý¾Ý¡£
£¨3£©ÕûºÏÅÌÎÊ
$user = User::field('name') ->join('profile', 'profile.user_id=user.id') ->select(); $profile = $user->profile;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý join() ÒªÁ콫 User ±íÓë Profile ±í¾ÙÐÐÅþÁ¬£¬È»ºó¿ÉÒÔÔÚ×ֶαí´ïʽÖлñÈ¡ Profile ±íµÄ×ֶΡ£
Ò»¶Ô¶à¹ØÁª
ÔÚ ThinkPHP 5.0 ÖУ¬Ò»¶Ô¶à¹ØÁªÍ¬ÑùÓÐÈýÖÖ·½·¨£º
£¨1£©¹ØÁªÄ£×ÓµÄÊôÐÔ
class User extends Model { public function books() { return $this->hasMany('Book'); } } class Book extends Model { public function user() { return $this->belongsTo('User'); } } $user = User::get(1); $books = $user->books;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý hasMany() ÒªÁ콫 User Ä£×ÓÓë Book Ä£×Ó¹ØÁªÆðÀ´£¬È»ºóŲÓà $user->books ÊôÐÔ»ñÈ¡¹ØÁªÊý¾Ý¡£
£¨2£©¹ØÁªÅÌÎÊ
$user = User::with('books')->select(); $books = $user->books;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý with() ÒªÁìÖ±½Ó¾ÙÐйØÁªÅÌÎÊ£¬È»ºóŲÓà $user->books ÊôÐÔ»ñÈ¡¹ØÁªÊý¾Ý¡£
£¨3£©ÕûºÏÅÌÎÊ
$user = User::field('name') ->join('book', 'book.user_id=user.id') ->select(); $books = $user->books;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý join() ÒªÁ콫 User ±íÓë Book ±í¾ÙÐÐÅþÁ¬£¬È»ºó¿ÉÒÔÔÚ×ֶαí´ïʽÖлñÈ¡ Book ±íµÄ×ֶΡ£
¶à¶Ô¶à¹ØÁª
¶à¶Ô¶à¹ØÁªÔÚ ThinkPHP 5.0 ÖÐͬÑùÓÐÈýÖÖ·½·¨£º
£¨1£©Ö÷Ä£×Ó¹ØÁªÄ£×ÓÊôÐÔ
class User extends Model { public function roles() { return $this->belongsToMany('Role'); } } class Role extends Model { public function users() { return $this->belongsToMany('User'); } } $user = User::get(1); $roles = $user->roles;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý belongsToMany() ÒªÁ콫 User Ä£×ÓÓë Role Ä£×Ó¹ØÁªÆðÀ´£¬È»ºóŲÓà $user->roles ÊôÐÔ»ñÈ¡¹ØÁªÊý¾Ý¡£
£¨2£©ÖÐÐÄ±í»®·ÖÅÌÎÊ
$user = User::get(1); $roles = $user->roles() ->where('status', '1') ->select();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Å²Óà $user->roles() ÒªÁì»ñÈ¡ÖÐÐıí, ½Ó×ÅʹÓà where() ÒªÁì¾ÙÐÐÌõ¼þÅÌÎÊ¡£
£¨3£©ÖÐÐıíÕûºÏÅÌÎÊ
$user = User::field('name,role.name as rolename') ->join('user_role','user_role.user_id=user.id') ->join('role', 'user_role.role_id=role.id') ->select(); $roles = $user->roles;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý join() ÒªÁ콫 User ±í¡¢UserRole ±íºÍ Role ±í¾ÙÐÐÅþÁ¬£¬È»ºó¿ÉÒÔÔÚ×ֶαí´ïʽÖлñÈ¡ Role ±íµÄ×ֶΡ£
ËÄ¡¢Ä£×ÓÊÂÎñ
ThinkPHP 5.0 Ä£×ÓÊÂÎñÔÚÄ£×ÓµÄÉúÃüÖÜÆÚÖÐÌṩÁËÐí¶àÓÐÓõĹ³×Ó£¬¿ÉÒÔÈÃÎÒÃÇÔÚ²î±ðµÄʱ¼äºÍ½×¶Î¶ÔÊý¾Ý¾ÙÐвÙ×÷ºÍ´¦Àí£¬¿ÉÒÔÀû±ãµØʵÏÖÊý¾ÝÑéÖ¤¡¢×Ô¶¯Ìî³ä¡¢Êý¾Ý¸üеȹ¦Ð§¡£³£ÓõÄÓÐÒÔÏÂÊÂÎñ£º
£¨1£©ÅÌÎÊÇ°ÊÂÎñ
class User extends Model { protected static function onBeforeFind($query) { // before find event } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý onBeforeFind() ÒªÁìÌí¼ÓÅÌÎÊÇ°ÊÂÎñ¡£
£¨2£©²åÈëÇ°ÊÂÎñ
class User extends Model { protected static function onBeforeInsert($data) { // before insert event } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý onBeforeInsert() ÒªÁìÌí¼Ó²åÈëÇ°ÊÂÎñ¡£
£¨3£©¸üÐÂÇ°ÊÂÎñ
class User extends Model { protected static function onBeforeUpdate($data) { // before update event } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý onBeforeUpdate() ÒªÁìÌí¼Ó¸üÐÂÇ°ÊÂÎñ¡£
£¨4£©É¾³ýÇ°ÊÂÎñ
class User extends Model { protected static function onBeforeDelete($data) { // before delete event } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Í¨¹ý onBeforeDelete() ÒªÁìÌí¼Óɾ³ýÇ°ÊÂÎñ¡£
Îå¡¢×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇ¿ÉÒÔ¿´µ½ ThinkPHP 5.0 ÖеÄÄ£×ÓʹÓúÜÊǼòÆÓ£¬Ö§³Ö CRUD ²Ù×÷ºÍ³£ÓõĹØÁªÅÌÎÊ¡£Í¬Ê±£¬Ä£×ÓÊÂÎñÄܹ»Àû±ãµØʵÏÖÊý¾ÝÑéÖ¤¡¢×Ô¶¯Ìî³ä¡¢Êý¾Ý¸üеȹ¦Ð§¡£Í¨¹ýÉîÈëѧϰģ×ÓµÄʹÓ㬿ÉÒÔÌá¸ß¿ª·¢Ð§ÂÊ£¬¼ÓËÙÏîÄ¿µÄ¿ª·¢Àú³Ì¡£
ÒÔÉϾÍÊÇÁÄÁÄThinkPHP 5.0 ÖÐÄ£×ÓµÄʹÓÃÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡