gpt4 book ai didi

php - CakePHP 通过 slug 而不是 ID 过滤列表

转载 作者:搜寻专家 更新时间:2023-10-30 20:12:13 25 4
gpt4 key购买 nike

我正在使用 CakePHP,这是我的数据库的结构:

CarMakes----------------------------------ID     Slug     Name16     ford     FordCarModels----------------------------------ID     Name     CarMake_ID10     Escort   16Cars----------------------------------ID     Name     CarModel_ID1      My car   10

我想查看 CarMakes.Slug 的汽车列表

因此 url 将是: http://localhost/cars/ford

任何想法或信息的一般方向?

最佳答案

您可以使用 findBy()findAllBy()根据 ID 以外的内容检索记录。如果您需要为查询提供条件,请使用常规 find() :

$this->Car->find(
'all',
array(
'conditions' => array(
'CarMake.Slug' => $slug,
'Car.Name LIKE' => $name
),
)
);

此外,对于您尝试设置的 URL,您需要创建一个 route对于/汽车:

Router::connect(
'/cars/:make',
array('controller' => 'cars', 'action' => 'bymake'),
array(
'pass' => array('make'),
'make' => '[A-Za-z]+'
)
);

编辑:

如果您的条件是基于与您的模型的直接关联,则上述方法有效。如果您的条件是递归关联(即 Car->CarModel->CarMake),您需要使用显式连接:

$result = $this->Car->find('all', array(
'joins' => array(
array(
'table' => 'car_models',
'type' => 'inner',
'foreignKey' => false,
'conditions' => array('car_models.id = Car.car_model_id')
),
array(
'table' => 'car_makes',
'type' => 'inner',
'foreignKey' => false,
'conditions' => array(
'car_makes.id = car_models.car_make_id',
'car_makes.slug' => $slug
)
)
),
'conditions' => array(
'Car.name LIKE' => $name
)
));

关于php - CakePHP 通过 slug 而不是 ID 过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7638433/

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