{"id":91,"date":"2010-03-24T17:55:48","date_gmt":"2010-03-24T23:55:48","guid":{"rendered":"http:\/\/www.curtisgibby.com\/blog\/91\/cakephp-containable-order\/"},"modified":"2011-02-28T07:27:56","modified_gmt":"2011-02-28T14:27:56","slug":"cakephp-containable-order","status":"publish","type":"post","link":"https:\/\/www.curtisgibby.com\/blog\/2010\/03\/cakephp-containable-order\/","title":{"rendered":"CakePHP containable order"},"content":{"rendered":"<p>I&#8217;m developing in CakePHP at work, and I was trying to do a sort on a  contained model. \u00a0 As in, for all of the <strong>Foo<\/strong>s, get all the  related <strong>Bar<\/strong>s, but order the Bars by Bar.publish_date (or  whatever).<\/p>\n<p>I kept getting conflicting information about whether that was even  possible and how to go about it. \u00a0 I also got the following error several  times: <em>Model &#8220;Foo&#8221; is not associated with model &#8220;order&#8221;<\/em> &#8212; it  thought I was trying to sub-contain &#8220;order&#8221; into Foo-&gt;Bar. \u00a0 I  eventually figured out that I had one too many parenthesis on the  contain conditions (i.e. which Bars to get). \u00a0 This is what I ended up  with that worked correctly.<\/p>\n<pre class=\"brush:php\">$this-&gt;Foo-&gt;contain(array(\r\n\t'Bar' =&gt; array(\r\n\t\t'conditions' =&gt; 'Bar.publish_date &lt;= \"'.date(\"Y-m-d H:i:s\",time()).'\"', \/\/ only get the bars that have been published\r\n\t\t'order' =&gt; 'Bar.publish_date asc' \/\/ sort bars by pubdate\r\n\t),\r\n));\r\n$foo = $this-&gt;Foo-&gt;findbyId($foo_id); \/\/ or whatever other kind of find you need<\/pre>\n<p>Hope this helps someone in the future!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m developing in CakePHP at work, and I was trying to do a sort on a contained model. \u00a0 As in, for all of the Foos, get all the related Bars, but order the Bars by Bar.publish_date (or whatever). I kept getting conflicting information about whether that was even possible and how to go about&hellip; <a class=\"more-link\" href=\"https:\/\/www.curtisgibby.com\/blog\/2010\/03\/cakephp-containable-order\/\">Continue reading <span class=\"screen-reader-text\">CakePHP containable order<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[15,16,156],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-programming","tag-cakephp","tag-php","tag-programming","entry"],"_links":{"self":[{"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":3,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions\/400"}],"wp:attachment":[{"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.curtisgibby.com\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}