{"id":568,"date":"2015-03-02T11:43:05","date_gmt":"2015-03-02T11:43:05","guid":{"rendered":"http:\/\/www.cakephpexpert.com\/blog\/?p=568"},"modified":"2015-03-05T07:03:14","modified_gmt":"2015-03-05T07:03:14","slug":"10-essential-tips-for-cakephp-development","status":"publish","type":"post","link":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development","title":{"rendered":"10 essential tips for CakePHP development"},"content":{"rendered":"<p>There are many things that come to you as surprise when you develop something. Of course, we are talking about the not-so-good surprises that baffle you at times with the kind of response you get while you are not at all ready for it. <!--more-->Based on my experiences I have a few tips to stem them up and get on with it easily. Here are the most common of them:<\/p>\n<p><a href=\"http:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-569\" src=\"http:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg\" alt=\"10 Basic Cakephp Tips\" width=\"740\" height=\"350\" srcset=\"https:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg 740w, https:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350-300x141.jpg 300w\" sizes=\"(max-width: 740px) 100vw, 740px\" \/><\/a><\/p>\n<p><strong>1. Not able to validate on create or update<\/strong><\/p>\n<p>Many a times we face a lot of difficulties with validation when we are just about to complete. This is really irritating and gets a hell out of you. Better if avoid it right away in the beginning when you start creating or updating a record file. For this, just put the <em>$validate<\/em> array on, when you plan to work on the records. This would save a lot of your efforts later on if any entry goes wrong.<\/p>\n<p><strong>2. Not able to get basic information about the table<\/strong><\/p>\n<p>There are lousy instances like this where you just go sick tumbling on little issues with nothing working for you. Hold on! Just try putting this: pr($this-&gt;ModelName-&gt;schema()) and you will get the information about the table. It is this easy!<\/p>\n<p><strong>3. Problem with Table Entry <\/strong><\/p>\n<p>Changes done on table can get a bit hard to manage for the program if they don\u2019t go as per the predefined format. You face situations when you go on doing changes on table entries and your app doesn\u2019t take them. All you need to do here is clear the cache. Delete the file from <em>app\/tmp\/cache<\/em> and you will get rid of all the strange things happening out there.<\/p>\n<p><strong>4. Accessing data session in the view<\/strong><\/p>\n<p>With the <em>$session<\/em> helper available, all you need to is just put in <em>pr($session). If there are already some variables placed in the session, <\/em>try <em>pr($session-&gt;read()) and you will be through with it.<\/em><\/p>\n<p><strong>5. Losing the extra URL parameters when paginating<\/strong><\/p>\n<p>This is normal when you are doing some random pagination task. For a URL, say, <em>\/products\/view\/45<\/em>, if you lose the ID (45) add this line of code to your view: <em>$paginator-&gt;options(array(\u2018url\u2019 =&gt; $this-&gt;passedArgs)). This will fetch you all the required details without a moment of strain.<\/em><\/p>\n<p><strong>6. Save() does not work!<\/strong><\/p>\n<p>It get annoying when you pass a command in <a title=\"Cakephp Development\" href=\"http:\/\/www.cakephpexpert.com\/\">CakePHP <\/a>and it does not work as per what your learning guide says. It happens with Save() a lot. If you ever face this error, go back and check the validations you have put. It is possible that some of the models your entered are failing to satisfy the validation rules and causing this error. An easy and fast way to do this is placing this command across <em>pr($this-&gt;validationErrors);<\/em> in view. This will instantly show what error it is and you will find an easy way out immediately.<\/p>\n<p><strong>7. Save() still does not work!<\/strong><\/p>\n<p>If you still see it not working, check if you have <em>beforeSave() <\/em>in your model or app model. Double-check for the \u2018true\u2019 value if you still can\u2019t see it. You will get it this time for sure!<\/p>\n<p><strong>8. Checking for a non-empty field in CakePHP 1.2<\/strong><\/p>\n<p>Well, now you have the<strong> \u2018<\/strong><em>notEmpty\u2019 rule built-in with the <a title=\"CakePHP 3.0: A Brief Introduction\" href=\"http:\/\/www.cakephpexpert.com\/blog\/cakephp-3-0-a-brief-introduction\">new CakePHP release<\/a>. So, the <\/em>old <em>VALID_NOT_EMPTY<\/em> constant is not favored anymore by the users. But, as you have no option to replace it with anything, it\u2019s better to use <em>\u2018rule\u2019 =&gt; array (\u2018minLength\u2019, \u20181\u2019) to get rid of it.<\/em><\/p>\n<p><strong>9. Using afterFind()<\/strong><\/p>\n<p>If in case you need to do some data manipulation and you are finding it difficult to do it using <em>afterFind()<\/em> do it after Model\u2019s find method, applying<em>Model::afterFind().<\/em><\/p>\n<p><em>These few lines of code would help you get it done:<\/em><\/p>\n<div style=\"background-color: #e0ecf8; float: left; width: 90%; padding: 10px 5%; margin-bottom: 20px; border: 1px solid;\">\n<p>functionafterFind($results, $primary=false) {if($primary== true) {<\/p>\n<p>\/\/ do stuff to the $results<\/p>\n<p>}<\/p>\n<p>return$results;<\/p>\n<p>}<\/p>\n<\/div>\n<p>Once you pass this code and the <em>find()<\/em> method is executed, the <em>$primary <\/em>function will be set to trueand the results will be returned back to your app.<\/p>\n<p><strong>10. You find it tough to build<strong> a form in CakePHP<\/strong><\/strong><\/p>\n<p><em>echo $form-&gt;create();<\/em><em><br \/>\n<em>echo $form-&gt;inputs();<\/em><br \/>\n<em>echo $form-&gt;end();<\/em><\/em><\/p>\n<p>Just enter this code and you have your CakePHP form in front of you. Just a step beyond scaffolding. Try it and you will have no complaints with form building any more.<\/p>\n<p><strong>Note: <em>T<strong>he tips described in this article is\u00a0only applicable for CakePHP version 1.x and latest 2.x versions of CakePHP.\u00a0<\/strong><\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many things that come to you as surprise when you develop something. Of course, we are talking about the not-so-good surprises that baffle you at times with the kind of response you get while you are not at all ready for it.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27,16],"tags":[72,71,73,38],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>10 essential tips for CakePHP development - Cakephp Expert Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"10 essential tips for CakePHP development - Cakephp Expert Blog\" \/>\n<meta property=\"og:description\" content=\"There are many things that come to you as surprise when you develop something. Of course, we are talking about the not-so-good surprises that baffle you at times with the kind of response you get while you are not at all ready for it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development\" \/>\n<meta property=\"og:site_name\" content=\"Cakephp Expert Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-03-02T11:43:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-03-05T07:03:14+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/#website\",\"url\":\"https:\/\/www.cakephpexpert.com\/blog\/\",\"name\":\"Cakephp Expert Blog\",\"description\":\"Cakephp Development Company\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.cakephpexpert.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg\",\"contentUrl\":\"http:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#webpage\",\"url\":\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development\",\"name\":\"10 essential tips for CakePHP development - Cakephp Expert Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#primaryimage\"},\"datePublished\":\"2015-03-02T11:43:05+00:00\",\"dateModified\":\"2015-03-05T07:03:14+00:00\",\"author\":{\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/#\/schema\/person\/0f12b00f339dcf099f8431b3ba0db2e1\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.cakephpexpert.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"10 essential tips for CakePHP development\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/#\/schema\/person\/0f12b00f339dcf099f8431b3ba0db2e1\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cakephpexpert.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28e9fe1b5e695a0ed4f8d10cb1186b26?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28e9fe1b5e695a0ed4f8d10cb1186b26?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\/\/www.cakephpexpert.com\/\"],\"url\":\"https:\/\/www.cakephpexpert.com\/blog\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"10 essential tips for CakePHP development - Cakephp Expert Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development","og_locale":"en_US","og_type":"article","og_title":"10 essential tips for CakePHP development - Cakephp Expert Blog","og_description":"There are many things that come to you as surprise when you develop something. Of course, we are talking about the not-so-good surprises that baffle you at times with the kind of response you get while you are not at all ready for it.","og_url":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development","og_site_name":"Cakephp Expert Blog","article_published_time":"2015-03-02T11:43:05+00:00","article_modified_time":"2015-03-05T07:03:14+00:00","og_image":[{"url":"http:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/www.cakephpexpert.com\/blog\/#website","url":"https:\/\/www.cakephpexpert.com\/blog\/","name":"Cakephp Expert Blog","description":"Cakephp Development Company","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.cakephpexpert.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#primaryimage","inLanguage":"en-US","url":"http:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg","contentUrl":"http:\/\/www.cakephpexpert.com\/blog\/wp-content\/uploads\/2015\/03\/10Basic-Tips-740x350.jpg"},{"@type":"WebPage","@id":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#webpage","url":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development","name":"10 essential tips for CakePHP development - Cakephp Expert Blog","isPartOf":{"@id":"https:\/\/www.cakephpexpert.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#primaryimage"},"datePublished":"2015-03-02T11:43:05+00:00","dateModified":"2015-03-05T07:03:14+00:00","author":{"@id":"https:\/\/www.cakephpexpert.com\/blog\/#\/schema\/person\/0f12b00f339dcf099f8431b3ba0db2e1"},"breadcrumb":{"@id":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.cakephpexpert.com\/blog\/10-essential-tips-for-cakephp-development#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.cakephpexpert.com\/blog"},{"@type":"ListItem","position":2,"name":"10 essential tips for CakePHP development"}]},{"@type":"Person","@id":"https:\/\/www.cakephpexpert.com\/blog\/#\/schema\/person\/0f12b00f339dcf099f8431b3ba0db2e1","name":"admin","image":{"@type":"ImageObject","@id":"https:\/\/www.cakephpexpert.com\/blog\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/28e9fe1b5e695a0ed4f8d10cb1186b26?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28e9fe1b5e695a0ed4f8d10cb1186b26?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/www.cakephpexpert.com\/"],"url":"https:\/\/www.cakephpexpert.com\/blog\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/posts\/568"}],"collection":[{"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/comments?post=568"}],"version-history":[{"count":6,"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/posts\/568\/revisions"}],"predecessor-version":[{"id":575,"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/posts\/568\/revisions\/575"}],"wp:attachment":[{"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/media?parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/categories?post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cakephpexpert.com\/blog\/wp-json\/wp\/v2\/tags?post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}