A
A
Alexander Borisovich2012-08-21 12:47:52
Yii
Alexander Borisovich, 2012-08-21 12:47:52

How to sort tables in Yii?

The "Users" table and the "Orders" table.
Users have many orders.
We display the last orders of the user in the table, so that sorting by them works.
Model "User" function relations

'lastorder'=>array(self::HAS_MANY,'Orders','userid','joinType'=>'INNER JOIN','limit'=>1, 'order'=>'date_time_reg desc'),<br>

Due to the fact that the code above returns an array with index 0, it is not possible to add it to the search()
function. This function works without problems with BELONGS_TO relationships.
But in the case of HAS_MANY , it returns an array, which search() does not work with.
$criteria->with = array('lastorder');// подключаем то что получили выше <br>
$criteria->compare('lastorder[0].name',$this->lastordername, false);<br>

lastorder [0] - we can't do that! How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kozhevnikov, 2012-08-21
@bethrezen

You need to add a variable to the user:
public $lastorderName;
In rules add lastorderName to safe.
In search it would be:
$criteria->compare('lastorder.name', $this->lastorderName, false);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question