Yii2-配置无效。必须设置“sql”属性

时间:2017-12-03 10:15:06

标签: php mysql session yii2

我正在尝试从网格视图中查看记录。但是,当我单击视图按钮时,会显示以下错误

  

无效配置 - yii \ base \ InvalidConfigException

     

必须设置“sql”属性。

错误来自我的一个控制器代码

$model->update();

        //$command = Yii::$app->db->createCommand("UPDATE ");

        $dataProvider = new SqlDataProvider([
            'sql' => $sql,// the error comes here
            'totalCount' => $count,
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

enter image description here

此外sqltotal count显示空记录。

另见下图

enter image description here

以下是我的代码。

查看代码

 if(empty($model))
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    else if($status=='Prepared')
    {

        $sql = "SELECT DISTINCT ogpd.`id`, ogpd.`meter_serial` AS 
        'Meter_Serial', IFNULL(ogpd.`remarks`,'') AS 'Remarks' FROM 
        `ogp_header` ogph
         INNER JOIN `ogp_detail` ogpd ON ogph.`id` = ogpd.`ogp_id` WHERE 
         ogpd.`ogp_id` = $ogp_id AND ogpd.`flag`=1 OR ogpd.`flag`=2 AND 
         ogph.`status` = 'Prepared'
         ORDER BY ogph.`id`";

        $count =Yii::$app->db->createCommand("SELECT COUNT(DISTINCT 
        ogpd.`id`) FROM `ogp_detail` ogpd
        INNER JOIN `ogp_header` ogph ON ogpd.`ogp_id` = ogph.`id` WHERE 
        ogpd.`ogp_id` = $ogp_id AND ogpd.`flag`=1 OR ogpd.`flag`=2 AND 
        ogph.`status` = 'Prepared'
        ORDER BY ogph.`id`")->queryScalar();

        $dataProvider = new SqlDataProvider([
            'sql' => $sql,
            'totalCount' => $count,
            'pagination' => [
                'pageSize' =>20,
            ],
        ]);

        return $this->render('view', [
            'dataProvider' => $dataProvider,
            'model' => $this->findModel($id),
            'id'=>$model->id
        ]);
    }
    else if($status == 'Created' || $status =='Printed')// I am at this point
    {
        $session = Yii::$app->session;
        $sql = $session->get('result');

        $session = Yii::$app->session;
        $count = $session->get('count');

        $model->update();

        //$command = Yii::$app->db->createCommand("UPDATE ");

        $dataProvider = new SqlDataProvider([
            'sql' => $sql,
            'totalCount' => $count,
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        return $this->redirect(Url::toRoute(['ogpheader/viewsetpdf','id' => $model->id, 'dataProvider'=>$dataProvider]));
    }
    else if($status =='Canceled')
    {
        $session = Yii::$app->session;
        $sql = $session->get('my_sql');

        $session = Yii::$app->session;
        $count = $session->get('total');

        $model->update();

        $dataProvider = new SqlDataProvider([
            'sql' => $sql,
            'totalCount' => $count,
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


        return $this->render('viewcreated', [
            'dataProvider' => $dataProvider,
            'model' => $this->findModel($id),
            'id' => $model->id
            /*'searchModel' => $searchModel*/
        ]);
    }

在我上面提到的代码中,我处于Printed状态,会话result在我的其他控制器中设置。

$query = "SELECT DISTINCT ogph.`id` AS 
'OGP_Serial_No',DATE(ogph.`created_at`) AS 'Date',uu.`name` AS 
'Created_By',ogpd.`meter_serial` AS 'Meter_Serial_Number',  u.`name` AS 
'Issued_To', ps.`name` AS 'Store',
IFNULL(ogph.`admin_incharge`,'') AS 'Admin_Incharge', 
IFNULL(ogph.`project_manager`,'') AS 'Project_Manager', 
IFNULL(ogph.`driver_name`,'') AS 'Driver_Name', IFNULL(ogpd.`remarks`,'') AS 
'Remarks'  
FROM `ogp_header` ogph 
INNER JOIN `ogp_detail` ogpd ON ogph.`id` = ogpd.`ogp_id` 
INNER JOIN `user` u ON ogph.`issuer` = u.`id`  
INNER JOIN `user` uu ON ogph.`created_by` = uu.`id`
INNER JOIN `project_store` ps ON ogph.`store_id` = ps.`id` $where AND 
ogph.`id` = $ogp_id";


    $session = Yii::$app->session;
    $session->set('result', $query);

    $count = Yii::$app->db->createCommand("SELECT COUNT(DISTINCT ogpd.`id`) 
    FROM `ogp_detail` ogpd
    INNER JOIN `ogp_header` ogph ON ogpd.`ogp_id` = ogph.`id` WHERE 
    ogph.`id`= $ogp_id")->queryScalar();

    $session = Yii::$app->session;
    $session->set('count', $count);

我不知道为什么会这样,因为它在我当地的网站上工作。

任何帮助都会得到高度赞赏。

0 个答案:

没有答案