zen of coding

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 »
%d bloggers like this: