Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP find all condition being ignored
    primarykey
    data
    text
    <p>I'm using CakePHP 1.3 and am having some trouble converting a SQL query to CakePHP. The concept behind the query is to retrieve all products associated with a specified account and belonging to a certain product category or the specified category's immediate child. </p> <p>There are three tables involved here, products, product_categories and category_types. product_categories is a link table that joins products to category_types. The category_types table is a tree structure that can join to itself. </p> <p>The SQL for the query should end up looking something like this if the category's id was 1 and the account's id was 1:</p> <pre><code>SELECT Product.id, Product.name, Product.account_id FROM products AS Product INNER JOIN product_categories AS pc ON (Product.id = pc.product_id) INNER JOIN category_types AS ct ON (ct.id = pc.category_type_id) WHERE ( ((Product.account_id IS NULL) OR (Product.account_id = '1')) AND ((ct.id = '1') OR (ct.parent_id = '1')) ) </code></pre> <p>Here is the code I'm using to try and do this, which is located in a function in my model:</p> <pre><code>$this-&gt;find('all', array( 'joins' =&gt; array( array( 'table' =&gt; 'product_categories', 'alias' =&gt; 'pc', 'type' =&gt; 'INNER', 'conditions' =&gt; array( 'Product.id = pc.product_id' ) ), // get category type array( 'table' =&gt; 'category_types', 'alias' =&gt; 'ct', 'type' =&gt; 'INNER', 'conditions' =&gt; array( 'pc.category_type_id = ct.id' ) ) ) , 'conditions'=&gt; array( 'OR' =&gt; array( 'Product.account_id IS NULL' , 'Product.account_id' =&gt; $account_id ) , 'OR' =&gt; array( 'ct.id' =&gt; $categoryTypeId , 'ct.parent_id' =&gt; $categoryTypeId ) ) )); </code></pre> <p>The problem is that the query is ignoring the account_id OR conditions resulting in SQL like this:</p> <pre><code>SELECT Product.id, Product.name, Product.account_id FROM products AS Product INNER JOIN product_categories AS pc ON (Product.id = pc.product_id) INNER JOIN category_types AS ct ON (ct.id = pc.category_type_id) WHERE ((ct.id = '1') OR (ct.parent_id = '1')) </code></pre> <p>How can I alter my php code to get the desired results?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload