{"id":2776,"date":"2022-08-30T15:27:41","date_gmt":"2022-08-30T15:27:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/15\/cakephp-multiple-model-conditions-in-find-with-habtm-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:41","modified_gmt":"2022-08-30T15:27:41","slug":"cakephp-multiple-model-conditions-in-find-with-habtm-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/cakephp-multiple-model-conditions-in-find-with-habtm-collection-of-common-programming-errors\/","title":{"rendered":"CakePHP multiple model conditions in find() with HABTM-Collection of common programming errors"},"content":{"rendered":"<p>My schema has the following relations:<\/p>\n<pre><code>User hasMany Transaction belongsTo User \nItem hasMany Transaction belongsTo Item\nUser hasManyAndBelongsTo Item using Transaction as join table \n<\/code><\/pre>\n<p>I&#8217;d like to return all items <strong>of a given item symbol<\/strong> that belong <strong>to a given user id<\/strong>. I&#8217;m able to retrieve all the items that belong to the user with<\/p>\n<pre><code>$this-&gt;User-&gt;find('all', array( 'conditions' =&gt; \n    array( 'User.id' =&gt; $userId ) ) )\n<\/code><\/pre>\n<p>and I&#8217;m able to retrieve all the Items with a given item name with<\/p>\n<pre><code>$this-&gt;Item-&gt;find( 'all', array( 'conditions =&gt;\n    array( 'Item.symbol' =&gt; $itemSymbol ) );\n<\/code><\/pre>\n<p>but if I try to use the condition array( &#8216;Item.symbol&#8217; =&gt; $itemSymbol, &#8216;User.id&#8217; =&gt; $userId ) on $this-&gt;Item-&gt;find(), $this-&gt;Item-&gt;User-&gt;find(), $this-&gt;User-&gt;find(), or $this-&gt;User-&gt;Item-&gt;find(), I get either the error<\/p>\n<pre><code>SQL Error: 1054: Unknown column 'Item.id' in 'where clause'\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>SQL Error: 1054: Unknown column 'Item.symbol' in 'where clause'\n<\/code><\/pre>\n<p>I&#8217;ve devoured HABTM in all versions of the cookbook and read tens of forum \/ SO posts about it, but I haven&#8217;t had any luck in forming this query.<\/p>\n<p>Relevant Info: \/\/ User.php associations<\/p>\n<pre><code>\/\/ a user hasMany transactions\n     var $hasMany = array('Transaction' =&gt;\n      array('className' =&gt; 'Transaction',\n      'order' =&gt; 'Transaction.created DESC', \/\/ order by descending time of transaction\n      \/\/'limit' =&gt; '1', \/\/ change this if we need more, for example, if we need a user transaction history\n      'foreignKey' =&gt; 'user_id',\n      )\n      ); \n    \/\/ a user hasAndBelongsTo items through transactions\n    var $hasAndBelongsToMany = array('Item' =&gt;\n        array('className' =&gt; 'Item',\n            'joinTable' =&gt; 'transactions',\n            'foreignKey' =&gt; 'user_id',\n            'associationForeignKey' =&gt; 'item_id',\n            'order' =&gt; '',\n            'limit' =&gt; '',\n            'unique' =&gt; true,\n            'finderQuery' =&gt; '',\n            'deleteQuery' =&gt; '',\n        )\n    );\n<\/code><\/pre>\n<p>\/\/ Item.php associations<\/p>\n<pre><code>var $hasMany = array('Transaction' =&gt;\n        array('className' =&gt; 'Transaction',\n            'order' =&gt; 'Transaction.created DESC',\n            \/\/'limit' =&gt; '1', \/\/ change this if we need more, for example, if we need a item transaction history\n            'foreignKey' =&gt; 'item_id',\n        )\n    );\n    \/\/ a item hasAndBelongsTo users through transactions\n    var $hasAndBelongsToMany = array('User' =&gt;\n        array('className' =&gt; 'User',\n            'joinTable' =&gt; 'transactions',\n            'foreignKey' =&gt; 'item_id',\n            'associationForeignKey' =&gt; 'user_id',\n            'order' =&gt; '',\n            'limit' =&gt; '',\n            'unique' =&gt; true,\n            'finderQuery' =&gt; '',\n            'deleteQuery' =&gt; '',\n        )\n    );\n<\/code><\/pre>\n<p>\/\/ Transaction.php associations:<\/p>\n<pre><code>var $belongsTo = array('User', 'Item');\n<\/code><\/pre>\n<ol>\n<li>\n<p>You&#8217;re looking for the Containable behavior; e.g.<\/p>\n<pre><code>\/\/ app_model.php\nvar $actsAs = array(\n    'Containable'\n);\n<\/code><\/pre>\n<p>I&#8217;m assuming User extends AppModel, so Containable will be set for the child (User). Now, in your controller, conditions for the related class can be placed in the &#8216;contain&#8217; key of the second argv of find(); e.g.<\/p>\n<pre><code>\/\/ users_controller.php\n$this-&gt;User-&gt;find('first', array(\n    'conditions' =&gt; array('User.id' =&gt; $userId),\n    'contain' =&gt; array(\n        'Item' =&gt; array(\n            'conditions' =&gt; array('Item.symbol' =&gt; $itemSymbol)\n        )\n    )\n);\n<\/code><\/pre>\n<p>Cake will find the matching User and retrieve matching Items (with the same user_id and the Item.Symbol specified. But be very careful to use &#8216;contain&#8217; =&gt; array() when it is not required, otherwise Cake will retrieve all of the defined associations, which will tax your database.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-15 11:35:12. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>My schema has the following relations: User hasMany Transaction belongsTo User Item hasMany Transaction belongsTo Item User hasManyAndBelongsTo Item using Transaction as join table I&#8217;d like to return all items of a given item symbol that belong to a given user id. I&#8217;m able to retrieve all the items that belong to the user with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2776","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2776"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2776\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}