ThinkPHP5ÖÐappend()ÒªÁìµÄʹÓÃ
thinkphpÊÇÒ»¿î»ùÓÚphpÓïÑԵĿª·¢¿ò¼Ü¡£ËüÌṩÁ˼òÆÓ¡¢ÓÅÑŵıà³ÌÌåÑéºÍÇ¿Ê¢µÄÀ©Õ¹ÐÔ£¬³ÉΪÁËphp¿ª·¢ÕßÃǵÄÊ×Ñ¡¿ò¼ÜÖ®Ò»¡£ÔÚthinkphpÖУ¬ÓÐÒ»¸ö·Ç¾³£ÓõÄÒªÁ졪¡ªappend()¡£±¾ÎĽ«ÏÈÈÝthinkphp5ÖÐappend()ÒªÁìµÄʹÓá£
Ò»¡¢Ê²Ã´ÊÇappend()ÒªÁì
ÔÚThinkPHP5ÖУ¬append()ÒªÁì¿ÉÒÔ×·¼ÓÒ»ÌõÊý¾Ýµ½Model¹¤¾ßÖС£appendÒªÁì¿ÉÒÔÔÚ²»±ØÅÌÎʵ½Ä¿½ñÄ£×Ó¹¤¾ßµÄÊý¾ÝµÄÇéÐÎÏ£¬¶ÔÆä¹ØÁªµÄÊý¾Ý¾ÙÐÐÐÔÌí¼Ó¡£appendÒªÁìÓï·¨ÈçÏ£º
public function appendRelation($relation, $data = [], $replace = false)
µÇ¼ºó¸´ÖÆ
ÉÏÊöÓï·¨Ú¹ÊÍÈçÏ£º
²ÎÊý | ˵Ã÷ |
---|---|
$relation | ¸Ã²ÎÊýÖ¸¶¨¹ØÁª¹ØϵÃû³Æ |
$data | ¸Ã²ÎÊýÖ¸¶¨ÒªÔöÌíµÄÊý¾Ý |
$replace | ¸Ã²ÎÊýÖ¸¶¨ÊÇ·ñÁýÕÖÒѱ£´æ¹ØÁªÊý¾Ý |
¶þ¡¢ÔõÑùʹÓÃappend()ÒªÁì
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃǾ³£ÐèÒªÔÚÒѾ±£´æµÄ¹ØÁª¹ØϵÖÐÌí¼Óмͼ¡£Õâʱ£¬ÎÒÃǾͿÉÒÔʹÓÃappend()ÒªÁì¡£ÏÂÃ棬ÎÒÃǼòÆÓÏÈÈÝÒ»ÏÂÔõÑùʹÓá£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÄ£×ÓÖнç˵ºÃËùҪ׷¼ÓµÄ¹ØÁª¹Øϵ£¬ºÃ±ÈÏÂÃæµÄUserÄ£×ÓÖеÄcity¹ØÁª¹Øϵ£º
class UserModel extends Model { public function city() { return $this->belongsTo('CityModel'); } }
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪʵÀý»¯Ò»¸öUser¹¤¾ß£º
$user = UserModel::get(1);
µÇ¼ºó¸´ÖÆ
È»ºóŲÓÃappend()ÒªÁ죬ÔöÌícity¹ØÁª¹ØϵµÄ¼Í¼£º
$user->appendRelation('city', [ 'id' => 100, 'city_name' => '¹ãÖÝ' ]);
µÇ¼ºó¸´ÖÆ
ÖÁ´Ë£¬ÎÒÃÇÀֳɵØÔÚUserÄ£×ÓµÄcity¹ØÁª¹ØϵÖÐÌí¼ÓÁËмͼ¡£ÈôÊÇÎÒÃǽÓ×ÅÓÖÏëÔöÌíÒ»Ìõ¼Í¼£¬Ö»ÐèÒªÔÙ´ÎŲÓÃappend()ÒªÁì¼´¿É£º
$user->appendRelation('city', [ 'id' => 101, 'city_name' => 'ÉîÛÚ' ]);
µÇ¼ºó¸´ÖÆ
Èý¡¢append()ÒªÁì×¢ÖØÊÂÏî
ÔÚʹÓÃappend()ÒªÁìµÄʱ¼ä£¬ÐèҪעÖØÒÔϼ¸µã£º
append()ÒªÁìÖ»ÊÇ×·¼ÓÒ»ÌõÊý¾Ýµ½¹ØÁª¹ØϵÖУ¬ÈôÒª¸ø¹ØÁª¹ØϵÖеĶà¸ö¼Í¼Ìí¼ÓÊý¾Ý£¬ÐèҪŲÓöà´Îappend()ÒªÁì¡£
append()ÒªÁì²»»á´¥·¢ÈκÎÊÂÎñ£¬Ò²²»»á´¥·¢¹ØÁªÊý¾ÝµÄÑéÖ¤¡£
append()ÒªÁìĬÈϲ»»áÁýÕÖÒѱ£´æµÄ¹ØÁªÊý¾Ý£¬ÈôÊÇÒªÁýÕÖÒѱ£´æµÄ¹ØÁªÊý¾Ý£¬ÐèÒª½«$replace²ÎÊýÉèΪtrue¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËThinkPHP5ÖÐappend()ÒªÁìµÄʹÓá£Í¨¹ýappend()ÒªÁ죬ÎÒÃÇ¿ÉÒÔÔÚ²»±ØÅÌÎʵ½Ä¿½ñÄ£×Ó¹¤¾ßµÄÊý¾ÝµÄÇéÐÎÏ£¬¶ÔÆä¹ØÁªµÄÊý¾Ý¾ÙÐÐÐÔÌí¼Ó¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÈôÊÇÎÒÃÇÏëÔÚÒÑÓеĹØÁª¹ØϵÖÐÌí¼Óмͼ£¬ÄÇôappend()ÒªÁìÊDz»¿Éȱ°±ÉÄÒªÁì¡£ÆÚÍû±¾ÎÄÄܹ»×ÊÖúµ½¸÷ÈË£¡
ÒÔÉϾÍÊÇThinkPHP5ÖÐappend()ÒªÁìµÄʹÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡