ThinkPHPµÄÅÌÎʹØÁª¹¦Ð§ÔõôÓÃ
Ò». Ä£×Ó¹ØÁª
1.1Ò»¶ÔÒ»¹ØÁª
Ò»¶ÔÒ»¹ØÁª¼´Á½¸öÊý¾Ý±íÖи÷×ÔÖ»ÓÐÒ»Ìõ¼Í¼£¬ÕâʱʹÓÃhasOne()ºÍbelongTo()º¯Êý¾ÙÐйØÁª¡£¼Ù¶¨ÎÒÃÇÓµÓÐÁ½¸öÊý¾Ý±í£¬Ò»¸ö³ÆΪÓû§±í(user table)£¬ÁíÒ»¸öÔòΪÓû§ÐÅÏ¢±í(userinfo table)£¬ËüÃǸ÷×ԵĽṹÈçÏ£º
user: id name userinfo: id user_id age
µÇ¼ºó¸´ÖÆ
ÉÏÃæÁ½¸ö±íÊÇͨ¹ý×Ö¶Îuser_id¾ÙÐйØÁªµÄ¡£ÏÖÔÚÎÒÃÇÐèÒªÔÚuser±íÖÐÅÌÎÊÓû§ÐÅÏ¢ºÍ¶ÔÓ¦µÄÄêË꣬°ì·¨ÈçÏ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚUserÄ£×ÓÖнç˵һ¸öuserinfo()ÒªÁ죬ҪÁìÃûËæÒâ¡£
//UserÄ£×Ó <?php class User extends Model{ public function userinfo(){ return $this -> hasOne('UserInfo', 'user_id'); } }
µÇ¼ºó¸´ÖÆ
ÔÚUserÄ£×ÓÖнç˵һ¸öage()ÒªÁ죬Õâ¸öÒªÁì×ÅʵÊǽç˵ÁËÒ»¸öÊôÐÔ£¬Ëü»á¼ûuserinfoÄ£×ÓµÄage×ֶΡ£
//UserÄ£×Ó <?php class User extends Model{ protected $readonly = ['age']; public function userinfo(){ return $this -> hasOne('UserInfo', 'user_id'); } public function getAgeAttr($value, $data){ if(isset($data['userinfo'])){ return $data['userinfo']['age']; } return ''; } }
µÇ¼ºó¸´ÖÆ
Íê³ÉÒÔÉÏ´úÂëºó£¬ÎÒÃǾͿÉÒÔʹÓÃfind()ÒªÁìÅÌÎʵ½ÎÒÃÇÏëÒªµÄÓû§ÒÔ¼°ÆäÄêË꣺
//ÅÌÎÊuser±íÖÐidΪ1µÄÓû§ $user = User::get(1); echo $user -> name; echo $user -> age;
µÇ¼ºó¸´ÖÆ
×¢ÖØ£ºÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁË$readonlyÊôÐÔ£¬$readonlyÊôÐÔÊÇThinkPHPÌṩµÄÊôÐÔ£¬ËûÄܹ»±£»¤Ò»Ð©ÊôÐÔ²»±»Ð´ÈëÊý¾Ý¿â¡£ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽ«ageÊôÐÔÉ趨Ϊֻ¶ÁÊôÐÔ£¬ÕâÑùµ±$user -> age±»»á¼ûµÄʱ¼ä£¬¾Í»á×Ô¶¯Å²ÓÃgetAgeAttrÒªÁìÈ¥ÅÌÎÊuserinfoÄ£×ÓÄÚÀïµÄage×ֶΡ£
1.2 Ò»¶Ô¶à¹ØÁª
Ò»¶Ô¶à¹ØÁªÖ¸µÄÊÇÁ½¸öÊý¾Ý±íÖÐÒ»¸öÓжàÌõ¼Í¼£¬Ò»¸öÖ»ÓÐÒ»Ìõ¼Í¼¡£ÈçÏÂÃæµÄÀý×Ó£º
order: id user_id order_no order_goods: id order_id name price
µÇ¼ºó¸´ÖÆ
ÉÏÃæÁ½¸ö±íÊÇͨ¹ý×Ö¶Îorder_id¹ØÁªµÄ¡£ÎÒÃÇÏÖÔÚÒª²éÕÒuser±íÖÐÓû§µÄ¶©µ¥ÐÅÏ¢ÒÔ¼°¶ÔÓ¦µÄÉÌÆ·ÐÅÏ¢£¬Ïêϸ²Ù×÷ÈçÏ£º
ÔÚUserÄ£×ÓÖнç˵һ¸öorders()ÒªÁ죬Õâ¸öÒªÁìÌåÏÖÒ»¸öÓû§Óжà¸ö¶©µ¥¡£
//UserÄ£×Ó <?php class User extends Model{ public function orders(){ return $this -> hasMany('Order', 'user_id'); } }
µÇ¼ºó¸´ÖÆ
ÔÚOrderÄ£×ÓÖнç˵һ¸ögoods()ÒªÁ죬Õâ¸öÒªÁìÌåÏÖÒ»¸ö¶©µ¥Óжà¸öÉÌÆ·¡£
//OrderÄ£×Ó <?php class Order extends Model{ public function goods(){ return $this -> hasMany('OrderGoods', 'order_id'); } }
µÇ¼ºó¸´ÖÆ
½ç˵ºÃÒÔÉϵĹØÁªºó£¬ÎÒÃǾͿÉÒÔʹÓÃfind()ÒªÁìÅÌÎʳöÓû§µÄ¶©µ¥ÒÔ¼°Ã¿¸ö¶©µ¥¶ÔÓ¦µÄÉÌÆ·£º
//ÅÌÎÊuser±íÖÐidΪ1µÄÓû§µÄ¶©µ¥ÐÅÏ¢ºÍ¶©µ¥µÄÉÌÆ·ÐÅÏ¢ $user = User::get(1, 'orders.goods'); var_dump($user -> orders[0] -> goods);
µÇ¼ºó¸´ÖÆ
×îºóÒ»¸ö²ÎÊý£¨’orders.goods’£©ÌåÏÖͬʱÅÌÎÊËüËùÓеÄOrderºÍOrder¹ØÁªµÄGoodsÐÅÏ¢¡£
¶þ. ÅÌÎʹØÁª
2.1 ʹÓùØÁªÅÌÎÊ
ÎÒÃÇ¿ÉÒÔͨ¹ý»á¼ûÄ£×Ó²ãÖнç˵µÄ¹ØÁªÊôÐÔ£¬ÊµÏÖ¹ØÁªÅÌÎÊ£¬¶øÎÞÐèÔÚÄ£×Ӳ㼶±ðÉϽç˵¹ØÁª¹Øϵ¡£È磬ÎÒÃÇÏÖÔÚÏëÅÌÎÊÒ»¸öÓû§ÒÔ¼°ËüµÄ¶©µ¥ÐÅÏ¢£º
$user = User::get(1); $orders = $user -> orders; echo $user -> name; foreach($orders as $order){ echo $order -> order_no . "\n"; }
µÇ¼ºó¸´ÖÆ
2.2 ÑÓ³Ù¹ØÁª
ÈôÊÇÎÒÃDz»ÏëÔÚÅÌÎÊÒ»¸öÄ£×ÓµÄʱ¼ä×Ô¶¯ÅÌÎÊËüµÄ¹ØÁª¹Øϵ£¬¿ÉÒÔʹÓÃÑÓ³Ù¹ØÁªÀ´ÊµÏÖ´ËÐèÇó¡£È磺
$user = User::with('orders')->get(1);
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÉèÖÃ$user±äÁ¿Ê±ÔÚwith()º¯ÊýÖнç˵ÁËËùÒª»ñÈ¡µÄ¹ØÁª¹Øϵ£¬ÕâʱÅÌÎÊÓï¾ä²»»áĬÈÏ×Ô¶¯ÅÌÎʹØÁª¹Øϵ£¬¶øÊÇÆÚ´ýÎÒÃÇʹÓùØÁª¹Øϵʱ²Å»áÅÌÎÊ¡£
2.3 °üÀ¨¹ØÁª
³ýÁËÉÏÃæµÄÑÓ³Ù¹ØÁªÍ⣬ÎÒÃÇ»¹¿ÉÒÔͨ¹ýÉèÖÃwithÒªÁìºóµÄtrue²ÎÊý×Ô¶¯°üÀ¨ËùÓйØÁª¹ØϵÀ´ÊµÏÖÎÒÃǵÄÅÌÎÊÐèÇó¡£È磺
$user = User::with('orders')->find(1, true);
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÔÚfind()ÒªÁìÖÐÔöÌíÁËÒ»¸ötrue²ÎÊý£¬Õâ¸ö²ÎÊýÌåÏÖÎÒÃÇÒª°üÀ¨userÄ£×ÓµÄËùÓйØÁª¹Øϵ¡£
ÒÔÉϾÍÊÇThinkPHPµÄÅÌÎʹØÁª¹¦Ð§ÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡