There is a request:

SELECT item.* FROM item JOIN price ON price.item = item.id JOIN currency ON currency.id = price.currency ORDER BY price.price * currency.course ASC 

It is necessary that it should produce not a simple array of arrays, but an array of objects of the Item class inherited from ActiveRecord in Yii2.

  • Please attach the existing PHP code. - Zhukov Roman
  • For example: $ posts = Yii :: $ app-> db-> createCommand ('SELECT item. * FROM item JOIN price ON price.item = item.id JOIN currency ON currency.id = price.currency ORDER BY price.price * currency.course ASC) -> queryAll (); So I get an array of arrays. And I need: $ item = Item :: find () -> all (); // so I’ll get everything, and I’ll get it in the query - Arzek

1 answer 1

Use the ActiveRecord :: findBySql () method:

 $items = Item::findBySql(" SELECT item.* FROM item JOIN price ON price.item = item.id JOIN currency ON currency.id = price.currency ORDER BY price.price * currency.course ASC ")->all(); 

Although in this particular case it is quite possible to make a sample through ActiveQuery

  • Thank you, that's what I asked) (How can I make a sample through ActiveQuery?) - Arzek