I make a filter for ads. There is such a point as the choice in which currency to display amounts.

How can the filter recalculate and display the result with recalculated values? That is, I have courses, I know how to recount. Question is it possible to make a request of this type?

if($this->course_type == Course::CONST_EUR){ $query->andFilterWhere(['object.price' * $course_value]); } 

It produces the error 'Operator '0' requires two operand , and I don’t think that the filter can recalculate and change the output data.

But there are no ideas yet, tell me how it can be implemented?

  • Yii2 did not study much, but in this case you multiply the object.price phrase by some value of $course_value (this is not necessary). And you need to do this: ['object.price' => $price * $rate] , that is, as far as I understand, the price column in the object table should be equal to the requested price multiplied by the exchange rate of the selected currency. - check1st
  • The crux of the matter is not that. How to recount I know. There is a table with currencies, and a course. In the database, all values ​​are usd by default. The problem is how to prescribe this arithmetic operation in the filter so that in the view it no longer displays the value from the database, but the recalculated - Vlad Shkuta
  • You have been described with your mistake (multiplying a string by something), what do you think any result might be there? The "andFilterWhere" method adds a value to the "WHERE" section of your query, again, what you end up with in the section that is responsible for filtering in the table, and not for the modification. Alternatively, in select write your multiplication 'SELECT object.price * 2.4 as price ...' - Bookin
  • Show what is called before in $query - check1st
  • There are many more type filters in $query - $query->andFilterWhere(['like', 'type_flat.id',$this->type_house]); , $query->andFilterWhere(['between', 'object.price', $this->price_from != null ? $this->price_from : 0, $this->price_to != null ? $this->price_to : $max_price]); etc. - Vlad Shkuta

2 answers 2

Depending on how you build the query, in the constructor you need to change the list of displayed fields, replacing the price something like this:

 $query->select("id, name, ..., (price * $rate) as price"); 
  • My query is built as follows: the search model passes, and adds the necessary conditions to the query - SELECT ... WHERE.... OR ( object . Type_id =7)) AND ( object_type . Parent_id =1)) AND ( object . Type_id =3)) AND ( commission_value ='0') . According to this scheme, you can only register this $query-select() at the very beginning, where is the query being formed? - Vlad Shkuta
  • @VladShkuta you can write it anywhere before the query is executed, it will override the SELECT - Bookin
  • @VladShkuta; If select not specified, then, most likely, by default it will display all possible fields from all tables participating in the query. - check1st
  • I'm doing something wrong .. The very first line, where the query $query = ObjectModel::find() . I write so - $query = ObjectModel::find()->select("(price * 5) AS price, *"); .. Error, the request does not go further. - Vlad Shkuta
  • The request more precisely goes further, but the dataProvider does not transfer data to the view, only the price value is Vlad Shkuta

Everything turned out to be easier, who needs it. There is such a method as addSelect http://www.yiiframework.com/doc-2.0/yii-db-query.html#addSelect addSelect ) addSelect Paste anywhere

 $query->addSelect(["object.*, round(price * $course_usd->course) AS price"]);