简体   繁体   English

Yii1多个联接到同一表

[英]Yii1 Multiple Joins to Same Table

I have the following relation in my Tickets Model: 我的票证模型具有以下关系:

'rl_status_id'=>array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('status_id'=>'related_id'),'condition'=>'model = "Tickets" AND field = "status_id"');    
'rl_category_id'=>array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('category_id'=>'related_id'),'condition'=>'model = "Tickets" AND field = "category_id"');  

I am trying to show a Cgridview that has sortable columns for the Category and Status. 我试图显示一个Cgridview,其中具有可分类的类别和状态列。

I am using the following to join the table: 我正在使用以下内容来加入表格:

$criteria->with = array('rl_category_id','rl_status_id');

I am also using the following CSort: 我也在使用以下CSort:

$sort = new CSort();
    $sort->attributes = array(
        Yii::t('app','model.tickets.category_id')=>array(
        'asc'=>'rl_category_id.related_value',
        'desc'=>'rl_category_id.related_value desc',
        ),
        Yii::t('app','model.tickets.status_id')=>array(
        'asc'=>'rl_status_id.related_value',
        'desc'=>'rl_status_id.related_value desc',
        ),

When accessing the page I get an error that a field in the related_fields table is ambigous, presumably because this is being joined twice effectively. 访问页面时,我得到一个错误,在一个领域related_fields表ambigous,大概是因为这是正在有效地两次加入。

In the tickets table the status ie numerical however, I wish to sort the column by the text in the related fields table ie Open as opposed to the numerical value in the column in tickets. 在票证表中,状态为数字,但是,我希望按相关字段表中的文本对列进行排序,即与票证中的列中的数值相反的是Open

By only using one in the Csort and the With the Grid sorting works as expected. 通过仅在CsortWith the Grid中使用一种,可以按预期进行排序。 But including a second from the same table causes a break. 但是在同一张表中加上一秒钟会导致中断。

I have found very little, but I understand you can add an alias to relation and also to the with. 我发现的很少,但是我知道您可以为关系以及with添加别名。 Therefore I have tried the following: 因此,我尝试了以下方法:

$criteria->with = array('rl_category_id'=>array('alias'=>'rl_category_id'),'rl_status_id'=>array('alias'=>'rl_status_id'));

'rl_status_id'=>array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('status_id'=>'related_id'),'condition'=>'model = "Tickets" AND field = "status_id"','alias'=>'rl_status_id');    
'rl_category_id'=>array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('category_id'=>'related_id'),'condition'=>'model = "Tickets" AND field = "category_id"','alias'=>'rl_category_id');    

But the issue still persist. 但是问题仍然存在。 I have tried with setting one of the above and both together. 我尝试将以上两者之一设置在一起。 I can confirm in my condition there is no mention of the issue field and I have removed all other conditions completely to ensure this is not the issue. 我可以确认我的情况没有提到问题字段,并且我已经完全删除了所有其他条件以确保这不是问题。

Does anyone have any suggestions on how to proceed? 有人对如何进行有任何建议吗?

Note 注意

I have included an answer of my own also as the situation is probably unique to me, but may help some others. 我也提供了自己的答案,因为这种情况可能对我来说很独特,但可能会对其他人有所帮助。

You could use alias for tables to disambiguish (last element of array): 您可以为表使用别名来消除歧义(数组的最后一个元素):

'rl_status_id'=>array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('status_id'=>'related_id'),'condition'=>'model = "Tickets" AND field = "status_id"', 'alias' => 'rlStatusId');    
'rl_category_id'=>array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('category_id'=>'related_id'),'condition'=>'model = "Tickets" AND field = "category_id"', 'alias' => 'rlCategoryId');  

Check also the documentation: 另请检查文档:

http://www.yiiframework.com/doc/guide/1.1/it/database.arr http://www.yiiframework.com/doc/guide/1.1/it/database.arr

I managed to find the issue and can confirm the aliasing does work as expected. 我设法找到问题,并且可以确认别名确实按预期工作。 My issue was actually in the relation and not the criteria of the dataprovider . 我的问题是实际上的关系,而不是criteria的的dataprovider

When building the relation I needed to change: 建立关系时,我需要更改:

'rl_category_id'=>array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('category_id'=>'related_id'),'condition'=>'model = "Tickets" AND field = "category_id"','alias'=>'rl_category_id');  

To

'rl_category_id'=>array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('category_id'=>'related_id'),'condition'=>'rl_category_id.model = "Tickets" AND rl_category_id.field = "category_id"','alias'=>'rl_category_id');  

The main difference being that I have filed names in the condition of the relation itself, model and field . 主要区别在于我已根据关系本身, modelfield的条件来命名。 By prefixing them in the condition also, this resolved the issue. 通过在条件中加上前缀,可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM