简体   繁体   English

yii CDbCriteria连接列未找到

[英]yii CDbCriteria Join Column not found

I've been struggling with converting the following SQL to CDBCriteria to be used with a CActiveDataProvider: "SELECT PresetDeviceLink. , Device. FROM PresetDeviceLink INNER JOIN Device ON Device.id = PresetDeviceLink.deviceId WHERE Device.roomId = 1" 我一直在努力将以下SQL转换为CDBCriteria以便与CActiveDataProvider一起使用:“ SELECT PresetDeviceLink。 Device。from PresetDeviceLink INNER JOIN Device ON Device.id = PresetDeviceLink.deviceId WHERE Device.roomId = 1”

Table structure is as follows: 表结构如下:

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       |                |
+-------------+--------------+------+-----+---------+----------------+

My code in my controller is as follows: 我在控制器中的代码如下:

$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,
));

When run I get the following error: 运行时,出现以下错误:

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 

The strange thing is, If I use 'Device' as the CActiveDataProvider source and change the join statement to join to 'PresetDeviceLink', it then complains that the Device.roomId column could not be found. 奇怪的是,如果我使用“设备”作为CActiveDataProvider源,并更改连接语句以连接到“ PresetDeviceLink”,则它会抱怨找不到Device.roomId列。

Do I just not understand how CActiveDataProvider works? 我只是不了解CActiveDataProvider是如何工作的? It looks to me that I can only use a condition (in a join or where clause) from a field in the table that I pass to the CActiveDataProvider. 在我看来,我只能在传递给CActiveDataProvider的表的字段中使用条件(在联接或where子句中)。 Any advice? 有什么建议吗?

PS - the SQL query works beautifully in the MySQL console. PS-SQL查询在MySQL控制台中可以很好地工作。

Thanks in advance, Ben 预先感谢,本

As is visible in the "The SQL statement executed was:" line, the first table was aliased to t . 在“执行的SQL语句为:”行中可以看到,第一个表的别名为t This is Yii's standard behaviour. 这是Yii的标准行为。

As a result of this, you should use that alias to refer to that table instead of PresetDeviceLink . 结果,您应该使用该别名而不是PresetDeviceLink来引用该表。 Or you could try setting $criteria->alias = 'PresetDeviceLink'; 或者您可以尝试设置$criteria->alias = 'PresetDeviceLink'; before using it in the CActiveDataProvider , though I have not personally tried that option, it should work. CActiveDataProvider使用它之前,尽管我没有亲自尝试过该选项,但它应该可以工作。

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

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