zen of coding

Dealing with calculated fields in CakePHP's find()

Let’s say we’ve got some calculated field in our find() method, something like SUM() or COUNT() or maybe AVG(). For the sake of example let’s do something simplistic, like: pr($this->Company->find(’all’, array(’fields’=>array(’Company.id’,’Company.name’, ‘COUNT(id) AS total_count’),                                […]

Read More »

Saving extra fields in the join table (for HABTM models)

An interesting question came up on IRC today, which essentially boils down to: “How to save extra fields in the join table for HABTM models, while creating a new record for one of the involved models all at once?” I’ve seen this question float around here and there, but do[…]

Read More »

SELECT … AS … in CakePHP 1.2

This is a simple hint, but hopefully will be useful to some… You might have noticed some examples where you have: $this->Profile->find(’all’, array(’fields’=>array(’SUM(Profile.votes) AS total_votes’))); But what if you simply needed Profile.field AS another_name? ‘fields’=>array(‘Profile.field AS another_name’) … isn’t going to work, just like that. What you need to do[…]

Read More »

Food for thought: $this->redirect() vs $this->render()

One example, that we often see, is something along the following lines in the controller: if($this->User->save($this->data)) {   $this->Session->setFlash(… some stuff for the view … );   $this->redirect(array(’action’=>’success’)); } If all we are doing is displaying a “success” page back to the user, do we really need to bother with[…]

Read More »

Food for thought: $this->redirect() vs $this->render()

One example, that we often see, is something along the following lines in the controller: if($this->User->save($this->data)) {   $this->Session->setFlash(… some stuff for the view … );   $this->redirect(array(’action’=>’success’)); } If all we are doing is displaying a “success” page back to the user, do we really need to bother with[…]

Read More »

Set::merge() and dynamic validation rules

Here’s another trick with Set::merge()… Let’s say we’ve defined some basic validation rules in our Profile model, something like: var $validate = array (    ‘name’ => array(       ‘rule’ => array(’notEmpty’),       ‘required’ => false,       ‘message’ => ‘Please enter a name’  […]

Read More »

Nice trick to toggle your Model field in CakePHP

This little trick will allow you toggle any field, for example ‘status’ which can be either 0 or 1… Add this little function to your model or better yet, app model: Update 1: Thanks to Kalt for the sound improvement to the original method Update 2: And thanks to rafaelbandeira3 for[…]

Read More »

Use Sanitize::html() class in the views

Update (9/16/2008): As some people pointed out it’s easier to use the h() method defined in basics.php of the core, it achieves the same basic goal, but since Sanitize class has other methods and purposes, I’m gong to leave this as an informational example and food for thought. (i.e. you[…]

Read More »

Use Set::merge() to modify the $paginate property

Let’s say you have a Posts controller, where you’ll need to paginate some posts in various actions. You might have some “generic settings” as the manual recommends in your $paginate variable similar to this: var $paginate = array(’Post’=>array(’limit’=>10, ‘order’=>array(’Post.modified DESC’))); Now, the above setting is probably going to work well[…]

Read More »

Book review: CakePHP Application Development

Nice folks over at Packt Publishing asked me to a do a review of a book they’ve recently published, called “CakePHP Application Development“, by Ahsanul Bari and Anupom Syam. I was quite happy to oblige and not only because I got a free book, but also because I’ve been meaning[…]

Read More »
%d bloggers like this: