CakePHP 查找具有相关模型条件的列表
我正在尝试获取 ID 列表(类似于 find('list')
),但无法弄清楚如何使用相关模型条件来执行此操作。
我的协会是: 产品 hasAndBelongsToMany 类别
产品属于制造商
我想查找类别 ID 列表中且具有特定制造商 seo_url 的产品 ID 列表。我已经尝试了大量包含条件、引用其他模型的条件,甚至尝试使用 find('all') 来查看我的条件是否有效,但无法得到任何结果。
我设法使用 Product->query() 获得一个,但它不是列表的格式。我可以循环遍历数组并获取 ID 或其他任何内容,但感觉非常不方便。我想确保 Cake 仍然对我的查询以及它所做的任何其他自动操作进行所有安全检查。这是我正在运行的查询:
$all_results = $return['Category']['all_results'] = $this->Product->query('
SELECT `Product`.`id`
FROM `categories_products` as `CategoriesProduct`
JOIN `products` as `Product` ON (`CategoriesProduct`.`product_id` = `Product`.`id`)
JOIN `manufacturers` as `Manufacturer` ON (`Product`.`manufacturer_id` = `Manufacturer`.`id` AND `Manufacturer`.`seo_url` = \''.$manufacturer.'\')
WHERE `CategoriesProduct`.`category_id` IN ('.implode(',', $search_categories).')
');
这会返回
[all_results] => Array
(
[0] => Array
(
[Product] => Array
(
[id] => 101787
)
)
[1] => Array
(
[Product] => Array
(
[id] => 100781
)
)
[2] => Array
(
[Product] => Array
(
[id] => 101887
)
)
[3] => Array
(
[Product] => Array
(
[id] => 101888
)
)
)
注意:$search_categories
是类别 ID 的列表(即:array(12,42,24,5)
)
I'm trying to get a list of IDs (a la find('list')
), but can't figure out how to do it with related model conditions.
My associations are:Product hasAndBelongsToMany Category
Product belongsTo Manufacturer
I want to find a list of product IDs that are in a list of category IDs and have a certain manufacturer seo_url. I've tried tons of contains with conditions, conditions referencing other models, even tried using find('all') to see if my conditions would work, but couldn't get anything.
I managed to get one with Product->query(), but it's not in the format of a list. I could cycle through the array and grab IDs or whatever, but it feels awfully un-Cakey. I want to make sure Cake still does all its security checks on my queries and whatever other automagic it does. Here's the query I got working:
$all_results = $return['Category']['all_results'] = $this->Product->query('
SELECT `Product`.`id`
FROM `categories_products` as `CategoriesProduct`
JOIN `products` as `Product` ON (`CategoriesProduct`.`product_id` = `Product`.`id`)
JOIN `manufacturers` as `Manufacturer` ON (`Product`.`manufacturer_id` = `Manufacturer`.`id` AND `Manufacturer`.`seo_url` = \''.$manufacturer.'\')
WHERE `CategoriesProduct`.`category_id` IN ('.implode(',', $search_categories).')
');
this returns
[all_results] => Array
(
[0] => Array
(
[Product] => Array
(
[id] => 101787
)
)
[1] => Array
(
[Product] => Array
(
[id] => 100781
)
)
[2] => Array
(
[Product] => Array
(
[id] => 101887
)
)
[3] => Array
(
[Product] => Array
(
[id] => 101888
)
)
)
Note: $search_categories
is the list of category ID's (ie: array(12,42,24,5)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要的结果的问题是,如果您在相关模型上使用条件,Cake 不会给您返回剥离的结果数组。
发生这种情况是因为 Cake 只会在相关模型上使用这些条件,并返回相关模型上条件成立的结果。
如果您想获取仅具有特定类别的产品,您需要通过类别模型进行查询,因为这使您可以在产品上使用条件。这可能看起来像这样:
这只会返回您想要的类别及其相关产品。
但是,如果您想要一个列表,这并不是很令人满意,因为您必须手动进行转换。
我宁愿看一下 Linkable Plugin ,它应该为您提供您想要的功能,因为它扩展了 Cake正如您在查询中所做的那样使用联接。这使得在相关模型的条件下获得结果成为可能。
The problem with your wanted result is, that Cake won't give you back a stripped result array if you use conditions on your related models.
This happens because Cake will only use these conditions on your related model and give you back the results with conditions being true on your related models.
If you want get back products with only specific Category you need to query through the Category-Model as this gives you the possibility to use the conditions on your Products. This could look something like this:
This will give you back only the wanted Category and its associated Products.
However, this is not very satisfying if you want a list, because you would have to manually do the conversion.
I would rather take a look at the Linkable Plugin which should give you exactly your wanted functionality as it extends Cake in using joins as you did in your query. This makes it possible on getting results with conditions on your related models.
CakePHP 对于相关模型的
条件
可能会很奇怪。尤其是与 HABTM 关系。即使将recursive
设置为最高值(即2)。查看文档以了解有关 HABTM 的更多详细信息。尝试以下操作。尽管从上面来看,我认为它不会起作用:
此外,尽可能避免
query()
。 MVC 的全部要点是将数据与显示与逻辑隔离。使用像query()
这样的东西只会打破这一点。CakePHP can be odd about
conditions
for related models. Especially with HABTM relationships. Even whenrecursive
is set to the highest value (i.e. 2). Check out the docs for more detail about HABTM.Try the following. Although from the above, I don't think it will work:
Also, avoid
query()
whenever possible. The whole point of MVC is to insulate data from display from logic. Using things likequery()
just breaks that.做一个正常的情况,然后转成列表格式。
Do a normal condition and change into list format after.