zen of coding

JSON output with CakePHP

Update (2/14/2011): Take a look at a more robust approach that works with recent 1.3 versions of CakePHP. Update (9/25/2008): Since the writing of this post, the CakePHP manual has been updated with information on using the RequstHandler, which is an excellent supplement to the approach described below. You should[…]

Read More »

JQuery autocomplete in CakePHP

Update to this post is here. (New libraries and new techniques are discussed). This is a quick example on how to setup an autocomplete field using JQuery and CakePHP. Let’s assume that we have a Product model and products controller. Our goal is to allow the user to type in[…]

Read More »

Multiple checkboxes

One question that I’ve seen come up a few times is: “How do I handle multiple checkboxes in a form?” The only trick here is the way you name your checkbox form element. Let’s consider an example where you’d like to select multiple messages (by using checkboxes) and then delete[…]

Read More »

Hooray JQuery

I’ve completely abandoned using Prototype/Scriptaculous in favor of JQuery. I simply prefer the way JQuery does things: Simple syntax No more in-line JS Lots of great plug-ins JQuery UI has some very nice widgets Lightweight In terms of CakePHP it means that I can no longer rely on the built-in[…]

Read More »

Excellent HTML table helper

If your application requires to display a lot of data from the DB, then I suggest you take a look at this nice, little HTML table helper: http://cakeforge.org/snippet/detail.php?type=snippet&id=162 Basically it will allow you to pass a data array from the find method and it will construct an HTML table with[…]

Read More »

CSS files and $scripts_for_layout

You probably know that if you have $scripts_for_layout in your site’s layout, then you can include JS files from the view. For example, if you’d like to include ‘myScript.js’ from some view you would add the following code: $javascript->link(‘myScript’, false); Basically the second param (false), will tell CakePHP not to include[…]

Read More »

Fatten up your Models

CakePHP models, unlike super models should be fat. It’s a good practice to keep redundant code out of your controllers. Here’s a quick example on how you can keep your controllers skinny and models fat. Let’s say you need to get a list of all public and active products… [sourcecode[…]

Read More »

Do I have any errors in my form?

A nice little trick to check if you have some errors in the view is to use the $session object. Try this (in the view): pr($this->validationErrors) or pr($session->validationErrors), be sure to actually have some errors in the form… I leave it up to you to come with some nice usage[…]

Read More »

You gotta love… Bindable behavior

Update: as of 05/18/2008 there is a new core Containable behavior, which is an evolution of a few previous behaviors, including Bindable. If you are just looking into starting to use this behavior I suggest you go with the Containable, since it’s going to be the new standard in the[…]

Read More »

A cleaner way to send emails

CakePHP 1.2 provides a very easy way to send emails using the built-in Email component. However, sending emails directly from your User’s controller add action, for example, is somewhat ugly. It’s better to write a custom method in your app controller to handle the sending of emails. Here’s an example, add[…]

Read More »
%d bloggers like this: