【问题标题】:Yii: Relational CActiveDataProvider does not working, got error "Column not found: 1054 Unknown column"Yii:关系 CActiveDataProvider 不起作用,出现错误“找不到列:1054 未知列”
【发布时间】:2015-10-26 06:49:39
【问题描述】:

我正在尝试以 CActiveDataProvider 样式获取数据,以便将数据传递给相应视图中的 CGridView。

我尝试以这种方式使用 CActiveData Provider 获取关系数据:

  1. 我有如下三张表:

    创建表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), 键fk_tlua_user (userId), 约束fk_tlua_location外键(locationId)参考tbl_test_locationlocationId)删除级联, 约束fk_tlua_user外键(userId)参考tbl_test_useruserId)删除级联 ) ENGINE=InnoDB 默认字符集=utf8;

  2. 模型中的关系是:

/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)'),
  1. 控制器中名为 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, )); }

  1. 视图是:

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 但我总是遇到同样的错误......

非常感谢您的帮助!

【问题讨论】:

  • 您可以尝试从条件中删除tbl_test_user。它可能有效...
  • 嗨@gamitg,谢谢,但如果我尝试这样做,我会收到另一个错误“完整性约束违规:where 子句中的 1052 列'userId'不明确。”此错误已解决,如在此处yiiframework.com/doc/guide/1.1/en/… 中解释的那样,在列名前加上其表的别名
  • 使用别名作为关系名称。

标签: php mysql yii cactivedataprovider


【解决方案1】:

正如你所看到的错误,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,
    ));
}

【讨论】:

    【解决方案2】:

    如果您想使用关系数据字段,只需使用关系名称和字段名称即可。

    $dataProvider=new CActiveDataProvider('TblTestLocation', array(
                'criteria'=>array(
                    'with'=>array(
                        'tblTestUsers'=>array(
                            'condition'=>'tblTestUsers.userId=1',
                        ),
                    ),
                ),
            ));
    

    这里“tblTestUsers”是模型类中定义的关系名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-05
      • 2020-05-14
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2021-08-08
      • 2019-11-11
      • 1970-01-01
      相关资源
      最近更新 更多