gpt4 book ai didi

php - 如何在 Cakephp 中按行 ID 从箭头检索特定行

转载 作者:行者123 更新时间:2023-11-29 21:28:18 25 4
gpt4 key购买 nike

下面的 cakephp 行从我的数据库中带回最高的 3 个 car_ids

$cars = $this->car->find('all',array('limit' => 3, 'order' => array('car.id' => 'desc')));

我想使用以下值具体说明我需要拉动哪 3 辆车:

car_id={3,7,10};

正确的命令是什么?

最佳答案

您需要在查询中使用 IN 条件。

在 MySQL 中,这看起来像:-

WHERE car.id IN (3, 7, 10)

在 CakePHP 2.x 中,您可以这样做:-

$cars = $this->car->find('all', array(
'conditions' => array(
'car.id' => array(3, 7, 10) // This is the `IN` condition
),
'limit' => 3,
'order' => array('car.id' => 'desc')
));

CakePHP 会自动推断您要使用 IN,因为条件中的值是一个数组。

在 CakePHP 3.x 中,我们需要 tell CakePHP to use IN这样它将把条件值转换为数组(否则,如果条件值不是 Cake 所期望的,您可能会收到错误):-

$cars = $this->car->find()
->where(['id IN' => [3, 7, 10]]);
->limit(3)
->order(['id' => 'DESC']);

关于php - 如何在 Cakephp 中按行 ID 从箭头检索特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35400843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com