CakePHP查找列表与相关的模型条件 [英] CakePHP find list with related model conditions

查看:104
本文介绍了CakePHP查找列表与相关的模型条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个ID列表(a find('list')),但无法弄清楚如何使用相关模型条件。



我的协会是:
产品hasAndBelongsToMany类别
产品属于制造商



我想找到一个产品ID列表,该产品ID在类别ID列表中,并且具有某个制造商seo_url。我试过吨的包含条件,条件引用其他模型,甚至尝试使用find('all')看看我的条件是否会工作,但不能得到任何东西。



我设法得到一个与Product-> query(),但它不是一个列表的格式。我可以循环通过数组和抓取ID或任何,但它感觉非常un-Cakey。我想确保蛋糕仍然对我的查询和任何其他自动化执行其所有的安全检查。这是我工作的查询:

  $ all_results = $ return ['Category'] ['all_results'] = $ this- >产品 - >查询('
SELECT'Product`.`id`
从'categories_products`为`CategoriesProduct`
JOIN`products`为`Product` ON .`product_id` =`Product`.`id`)
JOIN`manufacturer`为`Manufacturer` ON(`Product`.`manufacturer_id`=`Manufacturer`.```````````````seed_url` = \''。$ manufacturer.'\')
WHERE`CategoriesProduct`.`category_id` IN('.implode(',',$ search_categories)。)
');

这会返回

  [all_results] => Array 

[0] => Array

[Product] => Array

[id] => 101787




[1] => Array

[Product] => Array
b $ b [id] => 100781




[2] => Array
] => Array

[id] => 101887




[3] =&

[Product] => Array

[id] => 101888





注意: $ search_categories 是类别ID的列表(即: array(12,42,24,5)

解决方案

你想要的结果的问题是,如果你使用条件对你的相关模型,Cake不会给你一个剥离的结果数组。



发生这种情况是因为Cake只会在您的相关模型中使用这些条件,并返回您的相关模型中条件为真的结果。



回到仅具有特定类别的产品,您需要通过类别 - 模型查询,因为这使您可以使用产品上的条件。这看起来像这样:

  $ this-> Category-> find('all',array '=> array('Category.id'=> 2)); 

你回来只想要的类别及其相关产品
然而,如果你想要一个列表,这是不是很令人满意,因为你必须手动做转换。



我宁愿看看可链接插件,它应该能够为您提供所需的功能,扩展Cake使用连接,就像你在查询中一样,这使得有可能得到结果与相关模型的条件。


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))

解决方案

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->Category->find('all', array('conditions' => array('Category.id' => 2));

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查找列表与相关的模型条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆