×ðÁú¿­Ê±

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Ôõô¶à±íÅÌÎÊÓï¾äµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿