Yii多重关系 [英] Yii multiple relations

查看:35
本文介绍了Yii多重关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有 5 个表:

In my database I have 5 tables:

  • game(game_id,name,...)
  • 标签(tag_id,name,...)
  • 集合 (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.

我正在尝试使用 yii 关系执行此任务:

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')

然后我得到一个 $collection 并尝试这个:

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?

推荐答案

正如你在这里看到的:http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through,正确的该关系的声明如下:

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')

这篇关于Yii多重关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆