Php Yii使用CDbCriteria和CActiveDataProvider进行连接
这个问题似乎出现了很多,但是没有一个答案能帮助我解决问题 总结Php Yii使用CDbCriteria和CActiveDataProvider进行连接,php,yii,Php,Yii,这个问题似乎出现了很多,但是没有一个答案能帮助我解决问题 总结 我正在使用Yii创建一个应用程序 我有三个表,我正在尝试对其中两个表进行连接和筛选 我正在尝试使用CDbCriteria和CActiveDataProvider进行连接和筛选 我有所有表的模型,但是当我尝试加入它们时,我得到一个SQL错误 桌子 我已经为我想要加入和筛选的表创建了一个模型 记录 class Record extends CActiveRecord { public $owner; ...
- 我正在使用Yii创建一个应用程序李>
- 我有三个表,我正在尝试对其中两个表进行连接和筛选李>
- 我正在尝试使用CDbCriteria和CActiveDataProvider进行连接和筛选李>
- 我有所有表的模型,但是当我尝试加入它们时,我得到一个SQL错误
class Record extends CActiveRecord {
public $owner;
...
public function rules() {
return array(
array('given_name, family_name, dob, gender', 'required'),
array('qr_id, site_id', 'numerical', 'integerOnly' => true),
array('given_name, family_name, madin_name', 'length', 'max' => 100),
array('country_of_birth, country_of_death, title', 'length', 'max' => 45),
array('gender', 'length', 'max' => 5),
array('dod, profile, epitaph', 'safe'),
array('id, qr_id, given_name, family_name, madin_name, dob, dod, country_of_birth, country_of_death, gender, owner', 'safe', 'on' => 'search'),
);
}
...
public function relations() {
return array(
'families_left' => array(self::HAS_MANY, 'Family', 'record_left_id'),
'families_right' => array(self::HAS_MANY, 'Family', 'record_right_id'),
'headstones' => array(self::HAS_MANY, 'Headstone', 'record_id'),
'other_names' => array(self::HAS_MANY, 'OtherName', 'record_id'),
'users' => array(self::MANY_MANY, 'Users', 'record_owner(record_id, user_id)'),
'record_owner' => array(self::HAS_MANY, 'RecordOwner', 'record_id'),
);
}
...
}
记录所有者
class RecordOwner extends CActiveRecord {
...
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array();
}
...
}
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'record_owner.user_id' in 'where clause'. The SQL statement executed was: SELECT `t`.`id` AS `t0_c0`, `t`.`qr_id` AS `t0_c1`, `t`.`given_name` AS `t0_c2`, `t`.`family_name` AS `t0_c3`, `t`.`madin_name` AS `t0_c4`, `t`.`dob` AS `t0_c5`, `t`.`dod` AS `t0_c6`, `t`.`country_of_birth` AS `t0_c7`, `t`.`country_of_death` AS `t0_c8`, `t`.`gender` AS `t0_c9`, `t`.`profile` AS `t0_c10`, `t`.`epitaph` AS `t0_c11`, `t`.`site_id` AS `t0_c12`, `t`.`title` AS `t0_c13` FROM `record` `t` WHERE (record_owner.user_id LIKE :ycp0) ORDER BY `t`.`given_name` LIMIT 25
问题
我已经更新了搜索,在CDbCriteria中添加了记录所有者的条件,我已经添加了记录所有者的比较。用户id,但是现在收到SQL错误
搜索()
SQL错误
class RecordOwner extends CActiveRecord {
...
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array();
}
...
}
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'record_owner.user_id' in 'where clause'. The SQL statement executed was: SELECT `t`.`id` AS `t0_c0`, `t`.`qr_id` AS `t0_c1`, `t`.`given_name` AS `t0_c2`, `t`.`family_name` AS `t0_c3`, `t`.`madin_name` AS `t0_c4`, `t`.`dob` AS `t0_c5`, `t`.`dod` AS `t0_c6`, `t`.`country_of_birth` AS `t0_c7`, `t`.`country_of_death` AS `t0_c8`, `t`.`gender` AS `t0_c9`, `t`.`profile` AS `t0_c10`, `t`.`epitaph` AS `t0_c11`, `t`.`site_id` AS `t0_c12`, `t`.`title` AS `t0_c13` FROM `record` `t` WHERE (record_owner.user_id LIKE :ycp0) ORDER BY `t`.`given_name` LIMIT 25
问题:
我应该如何进行加入和筛选?Willem建议的解决方案。这是一条单行线 添加$criteria->together=true;返回search()方法
添加
$criteria->together=true
到搜索
方法
请看下面的说明:
特别是,
未设置此属性时,如果主表受到限制或
分页后,将为每个HAS\u MANY执行一条SQL语句
关系否则,将对所有对象执行单个SQL语句
由于您没有设置此值,并且正在使用分页,因此将通过对reach result的单独查询来获取记录的所有者
s。当然,假设查询实际完成了
通过设置
$criteria->together=true代码>强制将查询设置为单个查询,这是通过执行表联接完成的,这是您希望通过相关表中的一列筛选查询的方法。添加$criteria->together=true
转到search()
方法,看看这是否解决了问题。Willem,成功了。我离得很近,但离得很远。如果我能将我的解决方案作为答案发布,那么我会给你+1以上的答案,这样你就可以接受了。很高兴它成功了。为什么要创建第三个中间表呢?为什么不在记录表中保留用户id?