[英]Yii CDbCriteria select a field from a related object from another related object
Hi I need to create a CDbCriteria query with more complex relations. 嗨,我需要创建具有更复杂关系的CDbCriteria查询。
Table1 has a relation HAS_ONE to Table2 - let's call it Relation1 表1与表2的关系为HAS_ONE-我们称其为Relation1
Table2 has a relation HAS_ONE to Table3 - let's call it Relation2 表2与表3的关系为HAS_ONE-称之为关系2
Table3 has my desired Field let's call it Field2 Table3有我想要的字段,我们称它为Field2
$this refers to the class of Table1, where I defined $ this指向Table1的类,我在其中定义
class Table1 extends ActiveRecord {
public $Field1; // so I can do a search on it
...
public function mySearch()
{
$criteria = new CDbCriteria;
//I need to do sth like this:
$criteria->compare('Relation1.Relation2.Field2',$this->Field1);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
But with this I get an error, 但是我得到一个错误,
Column not found: 1054 Unknown column 'Relation1.Relation2.Field2' in 'where clause' 找不到列:1054“ where子句”中的未知列“ Relation1.Relation2.Field2”
Any ideas how to fix it? 任何想法如何解决? I do not want to write my own join, I want to do it the ORM way.
我不想编写自己的联接,我想使用ORM方式。
Use this: 用这个:
class Table1 extends ActiveRecord {
public $Field1;
public function mySearch()
{
$criteria = new CDbCriteria;
$criteria->with = array(
'relation_to_table2',
'relation_to_table2.relation_to_table3'
);
$criteria->compare('relation_to_table3.Field2',$this->Field1);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.