Multiple Relations

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?

You haven’t linked games back to tags.


'tags'=>array(self::MANY_MANY, 'Tag',  'collections_tags(coll_id,tag_id)'),  

'gameTags'=>array(self::MANY_MANY, 'GameTag', 'game_tag(tag_id,game_id)', 'through'=>'tags'),

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

I think that should at least put you on the right path.

Isn’t it enough that I have gameTags relation in the Game model?

Is it necessary to build the GameTag model?