thinkphpÔõô¶à±íÅÌÎÊÓï¾ä
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬Ô½À´Ô½¶àµÄÓ¦ÓóÌÐòÐèÒª¶ÔÊý¾Ý¿â¾ÙÐжà±íÅÌÎÊ£¬ÒÔʵÏÖÖØ´ó¹¦Ð§ÐèÇó¡£thinkphp×÷Ϊһ¿î¿ªÔ´µÄphp¿ª·¢¿ò¼Ü£¬ÌṩÁËÀû±ã¿ì½ÝµÄ¶à±íÅÌÎÊÓï¾ä£¬±¾ÎĽ«Îª¸÷ÈËÏÈÈÝthinkphpÖжà±íÅÌÎʵÄʵÏÖÒªÁì¡£
Ò»¡¢Á¬±íÅÌÎÊ
Á¬±íÅÌÎÊÒ²±»³ÆΪÍŽáÅÌÎÊ¡£µ±Ò»¸öÅÌÎÊ´Ó¶àÕűíÖлñÈ¡Êý¾Ýʱ£¬Õâ¸öÅÌÎʾÍÒªÓõ½ÅþÁ¬»òÕßÍŽáÅÌÎÊ¡£ÔÚthinkphpÖÐʹÓÃÁ¬±íÅÌÎÊ£¬ÐèҪʹÓÃDbÀàµÄjoin()ÒªÁì¡£
join()ÒªÁìµÄ²ÎÊýÈçÏ£º
$table£ºÒªÅþÁ¬µÄ±íÃû
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
$join£ºÅþÁ¬Ìõ¼þ
$type£ºÅþÁ¬ÀàÐÍ£¨Ä¬ÒÔΪ×óÅþÁ¬£©
¾Ù¸öÀý×Ó£ºÈôÊÇÎÒÃÇÏÖÔÚÐèÒª´Ó±íAÖлñÈ¡name×ֶκʹӱíBÖлñÈ¡status×ֶΣ¬ÒÔstatus×Ö¶ÎΪÌõ¼þÅþÁ¬Á½ÕÅ±í¡£¿ÉÒÔÕâÑùд£º
Db::name('A')->alias('a') ->join('B b','a.status=b.status') ->field('a.name,b.status') ->select();
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëÖУ¬ÎÒÃÇʹÓÃÁËDbÀàµÄjoin()ÒªÁ죬ָ¶¨ÁËÒªÅþÁ¬µÄ±íBºÍÅþÁ¬Ìõ¼þa.status=b.status¡£×¢Öص½ÕâÀïÎÒÃǸø±íAÉèÖÃÁËÓÖÃûa£¬ÕâÊǺÜÖ÷ÒªµÄ£¬ÓÉÓÚ´Ó±íBÖлñÈ¡×Ö¶Îstatusʱ£¬ÎÒÃÇÐèҪͨ¹ý±íÓÖÃûaÀ´Ö¸Ã÷»ñÈ¡ÄÄÕűíµÄstatus×ֶΡ£×îºó£¬ÎÒÃÇͨ¹ýfield()ÒªÁìÖ¸¶¨Òª»ñÈ¡µÄ×ֶΡ£
¶þ¡¢¶à±í¹ØÁªÅÌÎÊ
¶à±í¹ØÁªÅÌÎÊÊÇÖ¸ÔÚÅÌÎÊÀú³ÌÖУ¬°Ñ¶àÕűíµÄÊý¾Ý¾ÙÐнÏÁ¿£¬ÒÔij¸ö×Ö¶ÎÀ´½¨Éè¹ØÁªÐÔ£¬ÕâÐèÒªÓõ½¹ØÁªÅÌÎÊ¡£ÔÚthinkphpÖУ¬¹ØÁªÅÌÎÊÖ÷ÒªÓÐÒÔÏÂÈýÖÖ·½·¨£ºhasOne¡¢hasManyºÍbelongsToMany¡£
1.hasOne¹ØÁªÅÌÎÊ
hasOneÓÃÓÚÒ»¶ÔÒ»¹ØÁª£¬ÔÚÁ½ÕűíµÄ¹ØÁª×Ö¶ÎÉÏÉ趨¶ÔÓ¦¹Øϵ£¬¿ÉÒÔ½«Á½¸ö±íµÄÊý¾Ýƾ֤һ¶¨µÄ¹æÔòÅþÁ¬ÆðÀ´¡£ÎÒÃÇÀ´¿´Ò»¸öÀý×Ó£º
class UserModel extends Model { //hasOne¹ØÁªÅÌÎÊ public function order() { return $this->hasOne('OrderModel','user_id','id'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËhasOne¹ØÁªÅÌÎÊ£¬½«Ò»¸öÓû§¶ÔÓ¦Ò»¸ö¶©µ¥µÄÇéÐÎÅÌÎʳöÀ´ÁË¡£$this´ú±íÄ¿½ñÄ£×Ó£¬Ò»Ñùƽ³£¶¼ÊÇUserModel£¬order()ÒªÁìÊÇÎÒÃÇ×Ô½ç˵µÄÒªÁìÃû£¬·µ»ØֵʹÓÃÁËhasOneÒªÁì¾ÙÐйØÁªÅÌÎÊ¡£ÆäÖУ¬’OrderModel’Ö¸´úÎÒÃÇÒª¹ØÁªÅÌÎʵÄÄ£×Ó£¬’user_id’¡¢’id’»®·Ö´ú±íÁ½¸öÄ£×ÓÖ®¼äµÄ¹ØÁª×ֶΡ£
2.hasMany¹ØÁªÅÌÎÊ
hasManyÓÃÓÚÒ»¶Ô¶à¹ØÁª£¬ÔÚÁ½ÕűíµÄ¹ØÁª×Ö¶ÎÉÏÉ趨¶ÔÓ¦¹Øϵ£¬¿ÉÒÔ½«Ò»ÕűíºÍ¶àÕűíµÄÊý¾Ýƾ֤һ¶¨µÄ¹æÔòÅþÁ¬ÆðÀ´¡£ÎÒÃÇÀ´¿´Ò»¸öÀý×Ó£º
class UserModel extends Model { //hasMany¹ØÁªÅÌÎÊ public function order() { return $this->hasMany('OrderModel','user_id','id'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËhasMany¹ØÁªÅÌÎÊ£¬½«Ò»¸öÓû§¶ÔÓ¦¶à¸ö¶©µ¥µÄÇéÐÎÅÌÎʳöÀ´ÁË¡£$this´ú±íÄ¿½ñÄ£×Ó£¬order()ÒªÁìÊÇÎÒÃÇ×Ô½ç˵µÄÒªÁìÃû£¬·µ»ØֵʹÓÃÁËhasManyÒªÁì¾ÙÐйØÁªÅÌÎÊ¡£ÆäÖУ¬’OrderModel’Ö¸´úÎÒÃÇÒª¹ØÁªÅÌÎʵÄÄ£×Ó£¬’user_id’¡¢’id’»®·Ö´ú±íÁ½¸öÄ£×ÓÖ®¼äµÄ¹ØÁª×ֶΡ£
3.belongsToMany¹ØÁªÅÌÎÊ
belongsToManyÓÃÓÚ¶à¶Ô¶à¹ØÁª£¬ÔÚÖÐÑë±íµÄ¹ØÁª×Ö¶ÎÉÏÉ趨¶ÔÓ¦¹Øϵ£¬¿ÉÒÔ½«¶à¸ö±íµÄÊý¾Ýƾ֤һ¶¨µÄ¹æÔòÅþÁ¬ÆðÀ´¡£ÎÒÃÇÀ´¿´Ò»¸öÀý×Ó£º
class UserModel extends Model { //belongsToMany¹ØÁªÅÌÎÊ public function goods() { return $this->belongsToMany('GoodsModel','user_goods','goods_id','user_id'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËbelongsToMany¹ØÁªÅÌÎÊ£¬½«¶à¸öÓû§¶ÔÓ¦¶à¸öÉÌÆ·µÄÇéÐÎÅÌÎʳöÀ´ÁË¡£$this´ú±íÄ¿½ñÄ£×Ó£¬goods()ÒªÁìÊÇÎÒÃÇ×Ô½ç˵µÄÒªÁìÃû£¬·µ»ØֵʹÓÃÁËbelongsToManyÒªÁì¾ÙÐйØÁªÅÌÎÊ¡£ÆäÖУ¬’GoodsModel’Ö¸´úÎÒÃÇÒª¹ØÁªÅÌÎʵÄÄ£×Ó£¬’user_goods’Ö¸´úÖÐÑë±íµÄÃû³Æ£¬’goods_id’¡¢’user_id’»®·Ö´ú±íÖÐÑë±íºÍÁ½¸öÄ£×ÓÖ®¼äµÄ¹ØÁª×ֶΡ£
Èý¡¢×ܽá
±¾ÎÄÖ÷ÒªÏÈÈÝÁËthinkphpÖлùÓÚDbÀàµÄÁ¬±íÅÌÎÊÒÔ¼°¹ØÁªÅÌÎÊÒªÁì¡£ÔÚ¿ª·¢ÖУ¬ÎÒÃǾ³£ÐèÒª¶ÔÊý¾Ý¿â¾ÙÐжà±íÅÌÎÊ£¬Í¨¹ýѧϰ±¾ÎĵÄÄÚÈÝ£¬ÐÅÍÐÒѾÕÆÎÕÁËÏìÓ¦µÄ»ù´¡ÖªÊ¶¡£ÖµµÃ×¢ÖصÄÊÇ£¬Êý¾Ý±íÉè¼ÆÓ¦¸ÃºÏÀí£¬ÒÔ×î´ó»¯ïÔÌÊý¾ÝÈßÓàºÍÌá¸ßÅÌÎÊЧÂÊ¡£Òò´Ë£¬ÔÚ¾ÙÐжà±íÅÌÎÊʱ£¬ÎÒÃÇÒ²Ó¦¸Ã×ñÕÕÕâÒ»ÔÔò£¬¾¡¿ÉÄܵؽ«Êý¾Ý²ð·Ö³É¶à¸ö±í¾ÙÐд¦Àí¡£
ÒÔÉϾÍÊÇthinkphpÔõô¶à±íÅÌÎÊÓï¾äµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡