我正在尝试获取 CActiveDataProvider 样式的数据,以便将数据传递到相应 View 中的 CGridView。
我尝试以这种方式使用 CActiveData Provider 获取关系数据:
我有如下三个表:
创建表
tbl_test_location
(locationId
int(11) NOT NULL AUTO_INCREMENT,locationName
varchar(255) 默认为空, 主键 (locationId
) ) ENGINE=InnoDB AUTO_INCREMENT=6 默认字符集=utf8;创建表
tbl_test_user
(userId
int(11) NOT NULL AUTO_INCREMENT,userName
varchar(255) 默认为空, 主键 (userId
) ) ENGINE=InnoDB AUTO_INCREMENT=3 默认字符集=utf8;创建表
tbl_test_location_user_assignment
(locationId
int(11) 不为空,userId
int(11) 不为空, 主键 (locationId
,userId
), keyfk_tlua_user
(userId
), 约束fk_tlua_location
外键 (locationId
) 引用tbl_test_location
(locationId
) 删除级联, 约束fk_tlua_user
外键 (userId
) 引用tbl_test_user
(userId
) 删除级联 ) ENGINE=InnoDB 默认字符集=utf8;模型中的关系是:
/models/TestLocation.php
'tblTestUsers' => array(self::MANY_MANY, 'TestUser', '{{test_location_user_assignment}}(locationId, userId)'),
/models/TestUser.php
'tblTestLocations' => array(self::MANY_MANY, 'TestLocation', '{{test_location_user_assignment}}(userId, locationId)'),
- Controller 中名为 actionIndexOwn 的方法是: 注意:用户可以有多个位置,一个位置可以有多个用户。
/controllers/TestLocationController.php
public function actionIndexOwn()
{
$dataProvider=new CActiveDataProvider('TestLocation', array(
'criteria'=>array(
'with'=>array(
'tblTestUsers'=>array(
'condition'=>'tbl_test_user.userId=1',
),
),
),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
- View 是:
views/testLocation/index.php
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
我遇到了这个错误:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tbl_test_user.userId' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`locationId`) FROM `tbl_test_location` `t` LEFT OUTER JOIN `tbl_test_location_user_assignment` `tblTestUsers_tblTestUsers` ON (`t`.`locationId`=`tblTestUsers_tblTestUsers`.`locationId`) LEFT OUTER JOIN `tbl_test_user` `tblTestUsers` ON (`tblTestUsers`.`userId`=`tblTestUsers_tblTestUsers`.`userId`) WHERE (tbl_test_user.userId=1)
应用日志:
CDbCommand::fetchColumn() failed: SQLSTATE[42S22]: Column not found: 1054
Unknown column 'tbl_test_user.userId' in 'where clause'. The SQL statement
executed was: SELECT COUNT(DISTINCT `t`.`locationId`) FROM
`tbl_test_location` `t` LEFT OUTER JOIN
`tbl_test_location_user_assignment` `tblTestUsers_tblTestUsers` ON
(`t`.`locationId`=`tblTestUsers_tblTestUsers`.`locationId`) LEFT OUTER JOIN
`tbl_test_user` `tblTestUsers` ON
(`tblTestUsers`.`userId`=`tblTestUsers_tblTestUsers`.`userId`) WHERE
(tbl_test_user.userId=1).
in C:\htdocs\RackDomain\protected\views\testLocation\index.php (20)
in C:\htdocs\RackDomain\protected\controllers\TestLocationController.php
(147)
有人能帮帮我吗,我尝试了很多方法来编写关系 CActiveDataProvider,但我总是遇到同样的错误...
非常感谢您的帮助!
最佳答案
正如您在错误中看到的那样,Yii 在它的 sql 查询中使用了表名的别名。所以你也应该使用这个别名。更重要的是,将表名、列名和别名放在 ``
中更不容易出错。
您的代码应如下所示:
public function actionIndexOwn()
{
$dataProvider=new CActiveDataProvider('TestLocation', array(
'criteria'=>array(
'with'=>array(
'tblTestUsers'=>array(
'condition'=>'`tblTestUsers`.`userId`=1',
),
),
),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
关于php - Yii:关系 CActiveDataProvider 不工作,出现错误 "Column not found: 1054 Unknown column",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31799563/