YII:如何同时使用"with"和排序?



我在 YII 关系表上遇到了问题。

正如标题所说,我只想将表"A"与表"B"合并,并根据表"B"中的"clickTimes"对组合表进行排序。

$A = self::find()
-> with(['B'=>['order'=>'clickTimes DESC']])
-> all();

以上是我从互联网上学到的,但它没有奏效。错误如下。

PHP Warning – yiibaseErrorException
call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members

除排序外,该关系工作正常。有什么建议吗?非常感谢!

你可以像这样使用 CDbCriteria:

$criteria = new CDbCriteria();
$criteria->with = array('foreign_table1', 'foreign_table2',  'foreign_table2.foreign_table3');
$criteria->order = 'foreign_table3.col5 DESC';

你应该以正确的方式使用 with 和排序函数

$A = self::find()
-> with('your_relation_name')
->orderBy(['table_name.attribute'=>SORT_DESC])
->all();

最新更新