CDbCriteria 抛出列名称不明确

CDbCriteria throwing column name is ambigous

我的 Yii 应用程序的控制器中有以下方法。

public function actionManage($typeid=0, $locationid=0, $page=1, $rows=12, $sidx='date_input', $sord='desc', $kategori='')
    {
        if (Yii::app()->request->isAjaxRequest) {

            // Jika dilakukan operasi 'edit' pada row
            if (isset($_REQUEST['oper']))
            {
                $oper = $_REQUEST['oper'];
                $id = $_REQUEST['id'];
                if ($oper == 'edit')
                {
                    $value = $_REQUEST['value'];

                    $record = InputData::model()->findByPk($id);
                    $record->value = $value;
                    $record->update();
                }

                if($oper == 'delete'){
                    $model = InputData::model()->findByPk($id);
                    $model->delete();
                }
            }

            // inisialisasi criteria query
            $criteria = new CDbCriteria();
            $criteria->order = "$sidx $sord";

            // filter lokasi
            if (is_numeric($locationid) && $locationid !== 0)
            {
                $criteria->with = array('data'=>array(
                    'condition'=>'data.locationid=:locationid',
                    'params'=>array(':locationid'=>$locationid)
                ));
            } else {
                if (is_numeric($typeid) && $typeid !== 0)
                {
                    $criteria->with = array('data.location'=>array(
                        'with'=>array(
                            'type'=>array(
                                'condition'=>'type.typeid=:typeid',
                                'params'=>array(':typeid'=>$typeid)
                            )
                        )
                    ));
                } else {
                    $criteria->with = array('data.location'=>array(
                        'with'=>array(
                            'type'=>array(
                                'condition'=>'type.type_desc=:type_desc',
                                'params'=>array(':type_desc'=>$kategori)
                            )
                        )
                    ));
                }
            }

            // filter range tanggal
            if (isset($_REQUEST['startdate'], $_REQUEST['enddate']))
            {
                $startdate = $_REQUEST['startdate'];
                $enddate = $_REQUEST['enddate'];
                $criteria->condition = 'date_input <= :enddate AND date_input >= :startdate';
                $criteria->params = array(':startdate'=>$startdate, ':enddate'=>$enddate);
            }

            if(isset($_REQUEST['dataid'])){
                $dataid = $_REQUEST['dataid'];
                $criteria->addCondition("dataid = $dataid");
            }

            $dataProvider = new CActiveDataProvider('InputData', array(
                'criteria'=>$criteria,
                'pagination'=>array(
                    'currentPage'=>$page-1,
                    'pageSize'=>$rows
                )
            ));

            $count = $dataProvider->totalItemCount;
            $total_pages = $count > 0 ? ceil($count/$rows) : 0;
            if ($page > $total_pages) $page=$total_pages;

            // generate response untuk jqgrid
            $response = new stdClass();
            $response->page = $page;
            $response->total = $total_pages;
            $response->records = $count;

            foreach($dataProvider->getData() as $row)
            {
                $response->rows[] = array(
                    'id'=>$row->inputdataid,
                    'cell'=>array(
                        $row->inputdataid,
                        $row->date_input,
                        $row->time_input,
                        $row->data->location->location_name,
                        $row->data->data_name,
                        $row->data->variable->var_name,
                        round($row->value, 3),
                        $row->data->variable->unit->uom_name,
                        ($row->inputOfficer !== NULL ? $row->inputOfficer->officer_name:''),
                        '<a href="#" class="btn btn-default btn-xs edit-this-row"><i class="fa fa-pencil-square-o"></i> Edit</a> <a href="" class="btn btn-default btn-xs delete-this-row"><i class="fa fa-trash-o"></i> Delete</a>'
                    )
                );
            }
            echo json_encode($response);
        } else {
            $url = $this->createUrl("dataAir/manage");
            $delurl = $this->createUrl("dataAir/deleteRow");
            $startdate = '2013-01-01';
            $enddate = date_format(new DateTime(), 'Y-m-d');
            $this->render('jqgrid', array(
                'kategori'=>$kategori,
                'url'=>$url,
                'delurl'=>$delurl,
                'startdate'=>$startdate,
                'enddate'=>$enddate
            ));
        }
    }

这一行导致了问题

if(isset($_REQUEST['dataid'])){
    $dataid = $_REQUEST['dataid'];
    $criteria->addCondition("dataid = $dataid");
}

当我删除那些行时,该方法工作得很好。导致不明确的列名称的问题可能是什么?这是错误日志

SELECT COUNT(DISTINCT "t"."inputdataid") FROM "app_inputdata" "t"  LEFT OUTER JOIN "app_ref_periodicdata" "data" ON ("t"."dataid"="data"."dataid")  WHERE ((date_input <= :enddate AND date_input >= :startdate) AND (dataid = 7)) AND (data.locationid=:locationid). Bound with :startdate='2013-01-01', :enddate='2015-02-02', :locationid='6'

您需要在您的条件中添加 table 别名 t

$criteria->addCondition("t.dataid = $dataid");

此外,由于 $dataid 是从 $_REQUEST 获得的,因此最好将其作为参数传递。这可以通过两种方式完成:

$criteria->addCondition("t.dataid = :dataid", [":dataid" => $dataid]);

$criteria->compare("t.dataid", $dataid);

您使用了 table 个别名,

'with'=>array(
    'type'=>array(
    'condition'=>'type.type_desc=:type_desc', <<- I mean here you have used alias : type
    'params'=>array(':type_desc'=>$kategori)
 )

你只需要记住,你正在使用的主要模型总是需要别名 t 来消除歧义! (不确定那是不是一个真实的词 :D )