我想在自定义字段上对记录进行排序,但它在 yii2 中给我错误,任何人都可以帮助我。
->orderBy(player.PlayerRole, 'G','D','M','F');
其中 G、D、M 和 F 是自定义值。这在 mysql 中工作正常,但在 yii2 中却不行。
这个我也试过
$expression = new Expression('field(player.PlayerRole,G,D,M,F)');
->orderBy($expression)
这里是完整的查询
$expression = new Expression('field(player.PlayerRole,G,D,M,F)');
return (new Query())
->select('*')
->from('tablename')`enter code here`
->orderBy(player.PlayerRole, 'G','D','M','F');
->all();
最佳答案
如本期所述:
Please read the doc carefully. orderBy takes an array, and an array element can be an Expression. So you should use $query->orderBy(array($expression)) instead of $query->orderBy($expression).
https://github.com/yiisoft/yii2/issues/553
所以试试 ->orderBy(array($expression))
关于mysql - 按自定义值排序 Yii2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986132/