Yii multiple relations

荒凉一梦 提交于 2019-12-05 00:10:24

问题


In my database I have 5 tables:

  • game(game_id,name,...)
  • tag (tag_id,name,...)
  • collection (coll_id,name,...)
  • collections_tags (id,coll_id,tag_id)
  • game_tag (id,game_id,tag_id)

Every game has many tags, collection has many tags. If i take a collection, I can find its games using the collection's tags.

I'm trying to perform this task with yii relations:

//in Collection's relations:
 'tags'=>array(self::MANY_MANY, 'Tag',  'collections_tags(coll_id,tag_id)'),  
 'games'=>array(self::HAS_MANY, 'Game','tag_id', 'through'=>'tags')

Then I get a $collection and try this:

 echo "collection ".$collection->name.": (id=".$collection->coll_id.") has ".count($collection->tags)."tags\n";
echo count($coll->games);//error here

and get an error
What is wrong in the relations?


回答1:


As you may see here: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through, the correct declaration of that relation would be as follows:

 'games'=>array(self::HAS_MANY, 'Game', array('tag_id'=>'id'), 'through'=>'tags')


来源:https://stackoverflow.com/questions/9313259/yii-multiple-relations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!