CakePHP search list with matching model conditions

I am trying to get a list of identifiers (a la find('list') ), but I cannot figure out how to do this with the appropriate model conditions.

My associations: Product hasAndBelongsToMany Category Product belongsTo Manufacturer

I want to find a list of product identifiers that are in the list of category identifiers and have a specific manufacturer seo_url. I tried tons of content with conditions, conditions that link to other models, even tried to use find ('all') to see if my conditions worked, but couldn't get anything.

I managed to get one with Product-> query (), but it is not in list format. I could iterate over the array and grab identifiers or something else, but it feels terribly non-Cakey. I want to make sure Cake still performs all the security checks of my requests and any other automatic operations. Here is the request I received:

 $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).') '); 

it 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 a list of category identifiers (i.e.: array(12,42,24,5) )

+4
source share
2 answers

The problem with your desired result is that Cake will not return you the resulting array if you use conditions for its related models.

This is because Cake will use these conditions only for your sibling model and return the results, provided that the conditions match your native models.

If you want to return products with only a certain category, you need to request a model category, as this gives you the opportunity to use the conditions for your products. It might look something like this:

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

This will return you only the desired category and related products. However, this is not very nice if you need a list, because you have to manually perform the conversion.

I'd rather take a look at the Linkable Plugin , which should give you exactly the features you need, as it extends Cake when using joins, as you did in your request. This allows you to get results with conditions for your respective models.

+3
source

CakePHP may be odd relative to conditions for related models. Especially with HABTM relationships. Even when recursive set to the maximum value (i.e. 2). Check out the docs for more details on the HABTM .

Try the following. Although from the foregoing, I don't think this will work:

 $conditions = array('Category.id' => $category_ids, 'Manufacturer.seo_url' => $manufacturer); $this->Product('list', array('recursive' => 1, 'conditions' => $conditions)); 

Also, avoid query() whenever possible. The whole point of MVC is to isolate data from display from logic. Using things like query() just breaks this.

+3
source

All Articles