yii2正确的sql命令来执行我的查询

时间:2017-08-31 04:31:29

标签: yii2

我正试图从user_request_wallet表中获取数据,其中lead_developer =?和answer_tocall是?还是?我想加入mainRequest表格。

应该怎么命令???

我试过2路但没有成功

//$model = UserRequestWallet::find()->Where(['and',['lead_developer' => $getUser]])
//->andWhere(['and',['answer_tocall' => 'accepted']])
//->andWhere(['or',['answer_tocall' => 'putonhold']])
//->joinWith('mainRequest');

$model = (new \yii\db\Query())
->select('*')
->from('user_request_wallet')
->andwhere(['lead_developer'=> $getUser])->andWhere(['answer_tocall' => 'accepted'])
->orWhere(['answer_tocall'=>'putonhold'])->leftJoin('mainRequest');

标题说

Exception (Database Exception) 'yii\db\Exception' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE ((`lead_developer`=7) AND (`answer_tocall`='accepted')) OR (`answer_tocall' at line 1

正在执行的SQL是:

SELECT COUNT(*) FROM `user_request_wallet` LEFT JOIN `mainRequest` WHERE ((`lead_developer`=7) AND (`answer_tocall`='accepted')) OR (`answer_tocall`='putonhold')

3 个答案:

答案 0 :(得分:0)

这将是工作

$model = UserRequestWallet::find()
    ->where(['lead_developer' => $getUser])
    ->andWhere(['or', 'answer_tocall = accepted', 'answer_tocall = putonhold'])
    ->joinWith('mainRequest');

答案 1 :(得分:0)

$model = UserRequestWallet::find()
        ->with('mainRequest')
        ->where(['lead_developer' => $getUser])
        ->andWhere([
            'OR',
            ['answer_tocall' => 'accepted'],
            ['answer_tocall' => 'putonhold'],
        ])
        ->all();

答案 2 :(得分:0)

根据documentation,必须为函数leftJoin( $table, $on = '', $params = [] )再设置一个参数 如果您希望static method find()使用joinWith(['relationName'])方法ActiveRecords,则必须根据documentation正确定义模型中的关系。