PHP
·
发表于 5年以前
·
阅读量:8296
±³¾°£º
ÏÖÓÐ group £¨×e£(C)ºÍ user (Óû§) Á½ÖÖʵÀý¡£
Ò»¸o group ÖпÉÒÔÓµÓжa¸o user£¬Ò»¸o user Ò²¿ÉÒÔÊoÓÚÓжa¸o group £¨¶a¶Ô¶a¹Øϵ£(C)
GroupUserRelation ±iÓÃÓÚ°o¶¨×eºÏ×eÔ±µÄ¹Øϵ£¨Ê¹ÓÃid°o¶¨£(C)
×Ö¶ÎÓÐ id, group_id, user_id
ÏÖÓÐUser(Óû§)±i£¬ ÐeÒª»ñÈ¡ ÆaËuÊoµÄËuÓÐÓû§×eµÄÐÅÏ¢£¬ÐeҪʹÓÃhasMany()½øÐжa±i¹ØÁª¡£
User.id => GroupUserRelation.user_id
GroupUserRelation.group_id => Group.id
½¨1
public function getGroup()
{
return $this->hasMany(Group::className(), ['id' => 'group_id'])
->viaTable(GroupUserRelation::tableName(), ['user_id' => 'id']);
}
½¨2
public function getGroup()
{
return $this->hasMany(Group::className(), ['id' => 'group_id'])
->viaTable('groupUserRelation');
}
public function getGroupUserRelation()
{
return $this->hasMany(GroupUserRelation::tableName(), ['user_id' => 'id']);
}
ÒÔÉÏËuÊoÊÇС±a¸ø´o¼Ò½eÉܵÄYii2 hasOne(), hasMany() ʵÏÖÈý±i¹ØÁªµÄ½¨(Á½ÖÖ)£¬Ï£Íu¶Ô´o¼ÒÓÐËu°iÖu£¬Èç¹u´o¼ÒÓÐÈκÎÒÉÎÊÇe¸øÎÒÁoÑÔ£¬Ð¡±a»a¼°Ê±»Ø¸´´o¼ÒµÄ¡£ÔÚ´ËÒ²*dz£¸Ðл´o¼Ò¶Ô½Å±¾Ö®¼ÒÍøÕ¾µÄÖ§³Ö£¡