Newbie CakePHP Tips

Here are a few things to keep in mind when learning CakePHP.

  • Learn Cake’s naming conventions. If you don’t understand them, you will not understand cake. Simple. e.g. If you have a field dropdown field in a form, in your view you should use $form->input(’featured_status’), and in your controller you could use $this->set(’featuredStatuses’, array(0=>’Not featured’,1=>’Featured’) ); The naming convention automatically converts between then, so your featured_status field will be populated with all the relevant featuredStatuses.
  • Lean what “Fat Models, Thin Controllers” means. http://www.littlehart.net/atthekeyboard/2007/04/27/fat-models-skinny-controllers/
  • You don’t strictly need a separate controller for every single model. Often, it’s much better to have few er controllers which store all related actions. This often makes it a lot easier to locate relevant code. (e.g. If you had a fruit model, as well as models for oranges, apples, and bananas, it could be good to just have a single fruits_controller, rather than separate controllers for each one.
  • Code newsting inside views should generally be based on the HTML structure, rather than PHP structure. e.g. You should indent code based on div’s, rather than php ifs and loops.
  • You should NEVER use straight database queries. You should learn how to use model associations (hasMany, belongsTo, etc) to perform your queries. Be sure to learn bindModel and unbindModel to perform special quieries -> they allow you to dynamically change your model behaviours, but conveniently, they only last for one query, so your other code will not be affected.
  • Use a small number of standard elements to build most of your site. Add optional parameters for adding headings, links, rows of data etc. The fewer generic elements you can use to build your ENTIRE site the better.
  • Beware the dreaded self-closing div tags < div / >. You must always have a separate end tag for divs. Don’t know why, it’s just the rules. e.g. < div>< /div>
  • When doing browser compatability, comment out each element in turn to identify which elements are causing problems. Also, use Firebug and Internet Explorer Developer Toolbar to ensure your div nesting is correct => incorrect nesting is a major source of browser bugs, because Firefox is a lot friendler than IE, so if you develop and optimise in Firefox you layout may look right, but it may actually contain structural errors which really need to be fixed, rather than just using unreliable browser hacks to hide the problems.
  • IE is particularly picky when using AJAX -> perform W3C Validation frequently on your html/css, because it will often pick these errors up for you! Rather than digging around in your code for an elusive missing tag or incorrect nesting order.
  • When using Enum values, use this method to populated your form fields (by adding it to your app_model.php): e.g. Controller code: $this->Event->getEnumValues(’approval_status’)); http://bakery.cakephp.org/articles/view/baked-enums
  • You don’t need to include all tables in your $uses array. If you include one table, you can uses any models that are associated with that first model by accessing the sub-model as a child of the first model. e.g. If your controller users Author, but you want to use a Book model, you could use the format: $this->Author->Book->findById($book_id)

Bookmark This Page

Add 'Newbie CakePHP Tips' to Del.icio.us Add 'Newbie CakePHP Tips' to digg Add 'Newbie CakePHP Tips' to FURL Add 'Newbie CakePHP Tips' to blinklist Add 'Newbie CakePHP Tips' to My-Tuts Add 'Newbie CakePHP Tips' to reddit Add 'Newbie CakePHP Tips' to Feed Me Links! Add 'Newbie CakePHP Tips' to Technorati Add 'Newbie CakePHP Tips' to Socializer 

Leave a Reply


HOME | CONTACT Copyright © 2008 Extro Interactive