{"id":599,"date":"2014-01-10T12:07:48","date_gmt":"2014-01-10T12:07:48","guid":{"rendered":"http:\/\/thomas.w-p.me.uk\/blog\/?p=599"},"modified":"2014-01-10T12:54:05","modified_gmt":"2014-01-10T12:54:05","slug":"cakephp-eureka-moments","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2014\/01\/cakephp-eureka-moments\/","title":{"rendered":"CakePHP eureka moments"},"content":{"rendered":"<p>It is a very\u00a0 steep learning curve, and I keep forgetting what I learn, so just going to keep a list.<\/p>\n<ol>\n<li>To make a link to &#8216;id&#8217; on a .ctp page link to something more useful change the column name thus:\n<pre class=\"lang:default decode:true\">echo $this-&gt;Html-&gt;link($powerpoint['User']['id'], array('controller' =&gt; 'users', 'action' =&gt; 'view', $powerpoint['User']['id'])); \r\n\r\n\/\/becomes\r\n\r\necho $this-&gt;Html-&gt;link($powerpoint['User']['username'], array('controller' =&gt; 'users', 'action' =&gt; 'view', $powerpoint['User']['id']));<\/pre>\n<p>(The query is doing a join automatically in the background)<\/li>\n<li>To make a droplist show a useful thing like the name rather than the key, you need to go in to the XxxxxxController and change the function thus:\n<pre class=\"lang:default decode:true\">$users = $this-&gt;Powerpoint-&gt;User-&gt;find('list');\r\n\r\n\/\/becomes\r\n\r\n$users = $this-&gt;Powerpoint-&gt;User-&gt;find('list',array('fields' =&gt; array('User.id', 'User.username')));<\/pre>\n<p>I guess it then used the two fields as given.\u00a0 If you add a third field it is used as a label in the option.\u00a0 Not sure how to concatenate the columns if you want &#8220;Firstname Lastname&#8221; in the droplist.<\/li>\n<li>The Model reflects the database. I knew that but recording it here, as I actually used it.<\/li>\n<li>If you have a table that is joined through another table (belongsto &gt; belongsto) then you can make it appear thus in the Controller:\n<pre class=\"lang:default decode:true \">\t\t$users = $this-&gt;Question-&gt;Powerpoint-&gt;find('list', array(\r\n\t\t\t\t\"fields\" =&gt; array('Powerpoint.user_id', 'Users.username'),\r\n\t\t\t\t\"joins\" =&gt; array(\r\n\t\t\t\t\tarray(\r\n\t\t\t\t\t\t\"table\" =&gt; \"Users\",\r\n\t\t\t\t\t\t\"type\" =&gt; \"LEFT\",\r\n\t\t\t\t\t\t\"conditions\" =&gt; array(\r\n\t\t\t\t\t\t\t\"Powerpoint.user_id = users.id\"\r\n\t\t\t\t\t\t)\r\n\t\t\t\t\t)\r\n\t\t\t\t)\r\n\t\t\t));<\/pre>\n<p>&nbsp;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>It is a very\u00a0 steep learning curve, and I keep forgetting what I learn, so just going to keep a list. To make a link to &#8216;id&#8217; on a .ctp page link to something more useful change the column name thus: echo $this-&gt;Html-&gt;link($powerpoint[&#8216;User&#8217;][&#8216;id&#8217;], array(&#8216;controller&#8217; =&gt; &#8216;users&#8217;, &#8216;action&#8217; =&gt; &#8216;view&#8217;, $powerpoint[&#8216;User&#8217;][&#8216;id&#8217;])); \/\/becomes echo $this-&gt;Html-&gt;link($powerpoint[&#8216;User&#8217;][&#8216;username&#8217;], array(&#8216;controller&#8217; =&gt; &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2014\/01\/cakephp-eureka-moments\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CakePHP eureka moments<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[67],"class_list":["post-599","post","type-post","status-publish","format-standard","hentry","category-geekiness","tag-cakephp"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/comments?post=599"}],"version-history":[{"count":5,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":605,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/599\/revisions\/605"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}