2017-08-31 46 views
1

我想从user_request_wallet表中得到数据,其中lead_developer =?和answer_tocall是?或者是?我想加入mainRequest表。yii2 right sql命令来执行我的查询

应该如何命令???

我试过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') 
+0

'$ getUser'的值是多少?我认为它不是一个单一的价值。它的数组可能是 –

+0

它是一个数字它的7 – moh

+0

好吧,那么我认为不同类型的quotes'可能是一个问题。你可以尝试不用引号列或表名称? –

回答

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

这将是工作

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

Acording到documentation必须设置功能leftJoin($table, $on = '', $params = []) 一个更parametr如果你想使用static method find()ActiveRecords方法joinWith(['relationName'])必须通过正确的围定义模型关系,acording到documentation