1

Hi iam trying to covert this query as active record format

SELECT cmaindb, eshdr1, ascsnr, takpplz, takpadr
FROM ts_stats
 WHERE (cmaindb = 'tam' OR cmaindb = 'soc')
 AND (nproduktiv = 1) 
 OR  (cmaindb = 'tc') 
 AND (nabrfirm = 5) 
 AND (nproduktiv = 1)

code which i have tried

return self::find()
     ->andWhere(['or',
       ['cmaindb'=> 'tam'],
       ['cmaindb'=> 'soc']
     ])
     ->andWhere(['nproduktiv'=>1])
     ->orWhere(['cmaindb' => 'tc'])
     ->andWhere(['nabrfirm '=>5, 'nproduktivs'=>1])
     ->all();

this will fails

Shibon
  • 1,295
  • 1
  • 6
  • 19
  • [https://stackoverflow.com/questions/31557302/yii2-how-to-perform-where-and-or-or-condition-grouping](https://stackoverflow.com/questions/31557302/yii2-how-to-perform-where-and-or-or-condition-grouping) maybe this can help you – Sfili_81 Jul 10 '19 at 15:31

2 Answers2

2

Try this one

$cmaindbs = array('tam'=>'tam', 'soc'=>'soc');
return self::find()
     ->where(['IN', 'cmaindb' , $cmaindbs])
     ->andWhere(['nproduktiv' => 1])
     ->orWhere(['cmaindb' => 'tc'])
     ->andWhere(['nabrfirm' => 5])
     ->andWhere(['nproduktivs' => 1])
     ->all();
0

Let's further optimize your query:

return self::find()
     ->where(['IN', 'cmaindb' , ['tam', 'soc', 'tc'])
     ->andWhere(['nproduktiv' => 1])
     ->andWhere(['nabrfirm' => 5])
     ->andWhere(['nproduktivs' => 1])
     ->all();
WeSee
  • 2,032
  • 1
  • 18
  • 42