{"id":73,"date":"2011-09-28T01:19:35","date_gmt":"2011-09-28T01:19:35","guid":{"rendered":"http:\/\/info.ffteixeira.net\/2011\/09\/28\/retrieve-related-model-data-in-one-to-many-relation\/"},"modified":"2011-09-28T01:19:35","modified_gmt":"2011-09-28T01:19:35","slug":"retrieve-related-model-data-in-one-to-many-relation","status":"publish","type":"post","link":"https:\/\/blog.ffteixeira.net\/?p=73","title":{"rendered":"Retrieve Related Model Data in One-To-Many Relation"},"content":{"rendered":"<p>What Just Happened?<\/p>\n<p style=\"margin-top: 0.4em;margin-bottom: 0.8em\">The first line in the\u00a0<em>index()<\/em>\u00a0action of the AuthorsController sets the model attribute\u00a0<em><strong>$recursive<\/strong><\/em><strong>\u00a0<\/strong>to <strong>1<\/strong>.<br \/>\n<!--more--><br \/>\nWhat Just Happened?<\/p>\n<p style=\"margin-top: 0.4em;margin-bottom: 0.8em\">The first line in the\u00a0<em>index()<\/em>\u00a0action of the AuthorsController sets the model attribute\u00a0<em><strong>$recursive<\/strong><\/em><strong>\u00a0<\/strong>to <strong>1<\/strong>.<!--more--><\/p>\n<pre style=\"line-height: 1.5em;color: #006600;font-family: Courier, 'Courier New', sans-serif;background-color: #fafafa;margin-left: 40px;border: 1px solid #d1d7dc\">$this-&gt;Author-&gt;recursive = 1;<\/pre>\n<p style=\"margin-top: 0.4em;margin-bottom: 0.8em\">This special attribute is an integer that specifies the number of levels we want our model to fetch associated model data in the next\u00a0<em>find(&#8216;all&#8217;)<\/em>\u00a0or\u00a0<em>find(&#8216;first&#8217;)<\/em>\u00a0operations. In the AuthorsController, we set it to 1 for the\u00a0<em>Author<\/em>\u00a0model. This means the subsequent\u00a0<em>Author-&gt;find(&#8216;all&#8217;)<\/em>\u00a0operation will return all associated model data that are related directly to the\u00a0<em>Author<\/em>\u00a0model. The result of the function call\u00a0<em>Author-&gt;findAll()<\/em>\u00a0is then stored in a variable\u00a0<em>$authors<\/em>. The returned result is an array containing all authors and their book information.<\/p>\n<pre><em>Source:\u00a0<a href=\"http:\/\/www.packtpub.com\/article\/working-with-simple-associations-using-cakephp\">http:\/\/www.packtpub.com\/article\/working-with-simple-associations-using-cakephp<\/a><\/em><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What Just Happened? The first line in the\u00a0index()\u00a0action of the AuthorsController sets the model attribute\u00a0$recursive\u00a0to 1.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":0,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}