{"id":1561,"date":"2024-02-15T19:08:45","date_gmt":"2024-02-15T19:08:45","guid":{"rendered":"https:\/\/editjournal.redakt.eu\/faxmodem\/?p=1561"},"modified":"2024-02-15T19:08:45","modified_gmt":"2024-02-15T19:08:45","slug":"cakephp-find-auto-contain","status":"publish","type":"post","link":"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/cakephp-find-auto-contain\/","title":{"rendered":"Containing associated tables automatically"},"content":{"rendered":"<p>Say you have <code>OrdersTable<\/code> associated to <code>CustomersTable<\/code>, and you want every order you select to automatically contain the corresponding customer information. (So that you don't have to do <code>-&gt;contain(&#039;Customers&#039;)<\/code> on each <code>find()<\/code> call.)<\/p>\n<p>There are two ways to accomplish this.<\/p>\n<ol>\n<li>\n<p>In your <code>OrdersTable<\/code>:<\/p>\n<pre><code>public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)\n{\n    $query->contain('Customers');\n    return $query;\n}<\/code><\/pre>\n<\/li>\n<li>\n<p><a href=\"https:\/\/book.cakephp.org\/3\/en\/orm\/retrieving-data-and-resultsets.html#custom-finder-methods\">Use a custom finder<\/a><\/p>\n<\/li>\n<\/ol>\n<p>Further reading:<\/p>\n<ul>\n<li><a href=\"https:\/\/discourse.cakephp.org\/t\/contain-in-table-beforefind-sometimes-causes-issues-in-cakephp4-especially-with-muffin-slugbehavior\/10785\">https:\/\/discourse.cakephp.org\/t\/contain-in-table-beforefind-sometimes-causes-issues-in-cakephp4-especially-with-muffin-slugbehavior\/10785<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Say you have OrdersTable associated to CustomersTable, and you want every order you select to automatically contain the corresponding customer information. (So that you don&#8217;t have to do -&gt;contain(&#039;Customers&#039;) on each find() call.) There are two ways to accomplish this. In your OrdersTable: public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) { $query->contain(&#8216;Customers&#8217;); return&hellip; <a class=\"more-link\" href=\"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/cakephp-find-auto-contain\/\">Continue reading <span class=\"screen-reader-text\">Containing associated tables automatically<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1047],"tags":[715,1201],"class_list":["post-1561","post","type-post","status-publish","format-standard","hentry","category-cakephp","tag-cakephp","tag-cakephp-orm","entry"],"_links":{"self":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/comments?post=1561"}],"version-history":[{"count":1,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1561\/revisions"}],"predecessor-version":[{"id":1564,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1561\/revisions\/1564"}],"wp:attachment":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/media?parent=1561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/categories?post=1561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/tags?post=1561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}