【问题标题】:Order by specific field values in CakePHP在 CakePHP 中按特定字段值排序
【发布时间】:2020-09-15 17:45:54
【问题描述】:

我有数组 $productsTop

(int) 0 => '6
(int) 1 => '4',
(int) 2 => '1',
(int) 3 => '2',
(int) 4 => '3',
(int) 5 => '5'

其中 6 4 1 2 3 5 是产品表的 id。 我只想显示我的产品表中的 5 种产品,但不按 ID ASC 或 DSC 订购, 我想按 ID 订购它们,就像它们在 arrar 中订购的一样,所以它可以首先显示产品 ID 6,然后是 ID 4...到 ID 5。

有人可以帮忙吗?

$productTop = [6,4,1,2,3,5];

$product = $this->Product->find('all', array(
    'conditions'=>array('Product.id'=>$productTop),
    'limit'=>5
    ));

通过这种方式,它向我显示所有产品,但按其 ID 排序,而不是向我显示:

6,4,1,2,3,5

它显示:

1,2,3,4,5,6

【问题讨论】:

标签: php mysql cakephp


【解决方案1】:

您可以像这样按指定值排序:

'order' => "FIELD(id, '6', '4', '1', '3', '2', '5')"

【讨论】:

【解决方案2】:

你可以这样添加。 :)

->order(["FIELD(Product.id, '228')"=>'DESC', "Product.id"=>'DESC'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多