zen of coding

Dealing with errors in CakePHP

Chances are if your app is already out in production, you have your debug level set to ‘0’. This is probably good news, since the users will not be seeing any nasty errors and in the worst case, they’ll see a blank page or a ‘not found’ page in case[…]

Read More »

Dynamic menus without requestAction() in CakePHP 1.2

You’ve probably heard time and time again (especially in a few recent and popular blog posts) that using requestAction() is, generally speaking, considered to be a “bad practice”, “last resort”, “hackish” way of doing things in cake. Let’s consider one common use to see if we can achieve our goals[…]

Read More »

Controller makeover (from ugly to beautiful)

Let’s take a controller ridden with problems and see how we can improve it. Hopefully this little experiment can help you beautify your code and optimize your app… This is our ugly Users controller: class UsersController extends AppController { var $name = ‘Users’; var $uses = array(’User’, ‘Profile’); function add()[…]

Read More »

Automagical selects or checkboxes

A quick example of some automagic goodness… Let’s say we need to to build an “add Post” form and allow the user to pick some tags for the Post. In our Posts controller’s add() action, we’d probably do something like this: $this->set(’tags’, $this->Post->Tag->find(’list’, array(’fields’=>array(’Tag.tag’)))); Then in the view, while building[…]

Read More »

Filtering results returned by Containable behavior

First of all, let me warn you that what I’m describing here is more or less a hack and is not the most efficient way of handling such things, especially if you are dealing with large volumes of data. It is really here for educational purposes and as food for[…]

Read More »

HABTM and JOIN trickery with CakePHP

Update (10/7/2009)… this functionality has been rolled into Habtamable behavior An interesting question came up on IRC today… If I have Post HABTM Tag, how do I select only Posts that have Tags “new” AND “cakephp”? A seemingly simple problem, actually required a pretty tricky solution. I do have to[…]

Read More »

'fields', 'conditions' and associated models in CakePHP 1.2

Sometimes you see code that tries to do something like this: $this->Company->find(’all’, array(’conditions’ =>array(’Profile.name’=>’test’),                                             ‘fields’=>array(’Profile.id, Profile.name’))) Of course most of the time you’ll hear: “Well, this doesn’t work!”[…]

Read More »

Practical use of saveAll() (part 2, notes and tips)

In part 1, I explained how to use saveAll() in order to save multiple models at the same time, if you haven’t had a chance to read that post please do so before starting on this one. 1. Saving multiple records for the same model. Using our models from the[…]

Read More »

Practical use of saveAll() (part 1, working with multiple models)

(Part 2 is here) I would like to provide some insight on how to use the saveAll() method, with a few real-world examples. First, I’m going to cover how to use saveAll() when working with two related models. Imagine we are building a CRM of some sort. We have a[…]

Read More »
%d bloggers like this: