在这里使用CakePHP 2.0.标题有点误导,所以为了清理 - 我有会话数组,当用户将它们添加到购物车时,它会填充产品的ID.所以这个数组就像[0] => 25,[1] => 70等.假设这个数组叫做$ products.
我想问的是,有一些可能通过使用'find'模型的函数('conditions'=> array('Product.id'=> $ products))来获得数组,而不是通过一些模型进行排序.field值(如'order'选项),但是通过$ products数组索引,这样当我在视图中呈现产品内容时,我会将购物车中的所有产品按照用户添加它们的顺序排序.
这是一个例子 - Session数组$产品:
[0] => 35,
[1] => 15,
[2] => 25
Run Code Online (Sandbox Code Playgroud)
然后我将这个数组传递给find函数的条件:
$list = $this->Product->find('all', array('conditions' => array('Product.id' => $products)));
Run Code Online (Sandbox Code Playgroud)
最后$ list给出了按product.id排序的数组.所以不要:
[0] => Array
(
[Product] => Array
(
[id] => 35 )),
[1] => Array
(
[Product] => Array
(
[id] => 15 )),
[2] => Array
(
[Product] => Array
(
[id] => 25 ))
Run Code Online (Sandbox Code Playgroud)
我明白了:
[0] => Array
(
[Product] => Array
(
[id] => 15 )),
[1] => Array
(
[Product] => Array
(
[id] => 25 )),
[2] => Array
(
[Product] => Array
(
[id] => 35 ))
Run Code Online (Sandbox Code Playgroud)
到目前为止,所有答案都没有解决我的问题.请密切注意我给出的例子,这很简单,但提供的答案是不同的关键.
同样,我需要按照会话数组$ products的索引对最终数组$ list进行排序,但我得到按List.id字段排序的$ list,即使我根本没有指定任何甚至尝试将其设置为false.'find(array('order'=> ...))'
不想听起来粗鲁,但对我的问题的所有答复都是关于如何按产品 id 排序我的数组(这显然不是我要求的)或关于执行手动 sql 查询的建议,这是不合适的。看来我无法正确描述我需要什么,但我尽力让它尽可能清楚,抱歉。
我在尝试寻找如何在蛋糕中“禁用订单”时偶然发现了一个解决方案。因此,基本上您需要做的是拥有一个数组(我们称之为$queue),在将产品添加到购物车时将其保存在其中,然后在查找模型函数(或分页)的“order”选项中提供它喜欢:
'order'=>array('FIELD(Product.id,'.implode(',', $queue).')')
Run Code Online (Sandbox Code Playgroud)
$queue可能看起来像这样,例如:
[queue] => Array
(
[0] => 51
[1] => 1
[2] => 11
)
Run Code Online (Sandbox Code Playgroud)
最后你得到$list数组,其中产品的顺序与$queue中的顺序相同。如果您没有指定“order”,那么您将得到如下所示的$list数组:
[queue] => Array
(
[0] => 1
[1] => 11
[2] => 51
)
Run Code Online (Sandbox Code Playgroud)