在 Yii 中创建三个以上表的关系 [英] Create a relation of more than three tables in Yii

查看:23
本文介绍了在 Yii 中创建三个以上表的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 Yii 中创建超过三个表的关系时,我有以下方法给我带来了麻烦:

I have the following method that is causing me trouble as I try and create a relation of more than three tables in Yii:

public function relations()
{
     return array(
                 'info'=>array(self::BELONGS_TO, 'Software', 'ITEM_ID'),
                 'categories'=>array(self::MANY_MANY, 'ItemCategory',
                 'item_cat_relation(item_id, cat_id)',
                 'condition'=>'categories.cat_of_type=item_meta1.item_type_id'),
                 );
}

此代码在 item_meta1.item_type_id

推荐答案

一般来说,Yii 不是为处理三表关系"而构建的.话虽如此,您应该仍然能够在您的关系查询中添加一个条件(),我认为问题只是您没有加入 item_meta1 表.您可以通过两种方式执行此操作:

In general, Yii is not built to handle "three table relations". That being said, you should still be able to add a condition() to your relation query, I think the issue is just that you didn't JOIN the item_meta1 table. You can do this two ways:

1) 向您的关系添加 JOIN 子句:

1) Add a JOIN clause to your relation:

return array(
  'categories'=>array(self::MANY_MANY, 'ItemCategory',
    'item_cat_relation(item_id, cat_id)',
    'join'=>'JOIN item_meta1 ON categories.cat_of_type=item_meta1.item_type_id'
  ),
);

1) 向您的关系添加一个 WITH 子句(假设您为另一个表设置了一个关系):

1) Add a WITH clause to your relation (assuming you have a relation for the other table set up):

return array(
  'itemMeta'=>array(self::HAS_MANY, 'ItemMeta','item_type_id'), // I probably don't have this quite right, but you should get the idea
  'categories'=>array(self::MANY_MANY, 'ItemCategory',
    'item_cat_relation(item_id, cat_id)',
    'with'=>'itemMeta',
    'condition'=>'categories.cat_of_type=itemMeta.item_type_id')
  ),
);

我什至没有测试任何代码,但我做了类似的事情,所以原则上应该工作.:) 祝你好运!

I did not test any of that code even a little bit, but I have done similar things so it should work, in principle. :) Good luck!

这篇关于在 Yii 中创建三个以上表的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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