CDbCriteria抛出列名称不明确

时间:2015-02-02 10:37:31

标签: yii

我在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");
}

什么时候,我删除那些方法工作正常。什么可能导致暧昧的colum名称的问题?这是错误日志

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'

2 个答案:

答案 0 :(得分:1)

您需要将表格别名t添加到您的条件中:

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

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

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

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

答案 1 :(得分:0)

您使用了表别名,

'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)