[英]yii CDbCriteria Join Column not found
我一直在努力將以下SQL轉換為CDBCriteria以便與CActiveDataProvider一起使用:“ SELECT PresetDeviceLink。 , Device。from PresetDeviceLink INNER JOIN Device ON Device.id = PresetDeviceLink.deviceId WHERE Device.roomId = 1”
表結構如下:
mysql> describe PresetDeviceLink;
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| presetId | int(11) | NO | | NULL | |
| deviceId | int(11) | NO | | NULL | |
| state | int(11) | NO | | 0 | |
| value | int(11) | NO | | 32 | |
+----------+---------+------+-----+---------+----------------+
mysql> describe Device;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ref | int(11) | NO | | NULL | |
| roomId | int(11) | NO | | NULL | |
| typeId | int(11) | NO | | NULL | |
| paired | tinyint(1) | NO | | 0 | |
| name | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| dimmerPos | int(11) | NO | | 0 | |
+-------------+--------------+------+-----+---------+----------------+
我在控制器中的代碼如下:
$criteria = new CDbCriteria;
$criteria->select = 'PresetDeviceLink.*, Device.*';
$criteria->join = 'INNER JOIN Device ON Device.id = PresetDeviceLink.deviceId';
$criteria->condition = 'Device.roomId = 1';
$presetDeviceLink=new CActiveDataProvider('PresetDeviceLink', array(
'criteria' => $criteria,
));
運行時,出現以下錯誤:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: <b>Column not
found</b>: 1054 Unknown column 'PresetDeviceLink.deviceId' in 'on clause'. The SQL
statement executed was: SELECT COUNT(*) FROM `PresetDeviceLink` `t` INNER JOIN
Device ON Device.id = PresetDeviceLink.deviceId WHERE Device.roomId = 1
奇怪的是,如果我使用“設備”作為CActiveDataProvider源,並更改連接語句以連接到“ PresetDeviceLink”,則它會抱怨找不到Device.roomId列。
我只是不了解CActiveDataProvider是如何工作的? 在我看來,我只能在傳遞給CActiveDataProvider的表的字段中使用條件(在聯接或where子句中)。 有什么建議嗎?
PS-SQL查詢在MySQL控制台中可以很好地工作。
預先感謝,本
在“執行的SQL語句為:”行中可以看到,第一個表的別名為t
。 這是Yii的標准行為。
結果,您應該使用該別名而不是PresetDeviceLink
來引用該表。 或者您可以嘗試設置$criteria->alias = 'PresetDeviceLink';
在CActiveDataProvider
使用它之前,盡管我沒有親自嘗試過該選項,但它應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.