zen of coding

BETWEEN AND SQL syntax in CakePHP

This is how you’d write a BETWEEN… AND… SQL query in a cake-like way. The example should be pretty much self explanatory: $this->Post->find(‘all’, array(‘conditions’=>array(‘Post.id BETWEEN ? AND ?’ => array(1, 10)))); Note, that CakePHP will quote the numeric values depending on the field type in your DB.

Read More »

CakePHP and save() security

An interesting point came up on IRC… What happens if someone submits data to your application via a fake form? How can you ensure that a malicious user will not simply save some unwanted data by filling your $this->data array with things you don’t want there? For example, by sending[…]

Read More »

Retaining a search string in the URL

As you know, for a good reason, CakePHP will use the POST method by default to submit form data. However, there is a case where this could present a problem… Let’s say you are building a little search tool for all of your articles. It would be nice to allow[…]

Read More »

read() vs find()

What exactly is the differednce between $this->User->read() and $this->User->find()? Seemingly, they both retrieve the data (and possibly associated data) for the User model. Yet, there is a an important distinction: read() will also set the data for your model object, while find() will simply return the resultset array. What’s the[…]

Read More »

Changes to database config keys in RC1

There are a few important changes in the database configuration, which I wanted to quickly point out: 1. ‘connect’ key is gone. For example, if you had ‘connect’ => ‘mysql_connect’ you will need to replace it with ‘driver’=>’mysql’ 2. New key: ‘persistent’=>false This one is very important and you definitely[…]

Read More »

Validating a checkbox in CakePHP 1.2

In a few simple words: use the ‘comparison’ rule to validate a checkbox. To give you an example, let’s say a user needs to agree to the terms of service when registering a new account. In your User model you setup a rule for the checkbox as follows: [cc lang=”php”][…]

Read More »

15 Essential CakePHP Tips

Note, this article was written a long time ago for CakePHP version 1.x … many of the points described here still apply in the latest 2.x versions of CakePHP. 1. Save() does not work! Sometimes it happens that save() fails without any obvious reason. Your data array looks fine and[…]

Read More »

Set variable in the view to be used in the layout

I’m not sure that you should really be doing this, but I recall running into one situation where I wanted to set some variable in the view and have it available in the layout. Here’s how… In the view: $this->viewVars[‘myVar’] = ‘something’; In the layout: echo $myVar; Nate pointed out[…]

Read More »

New way to GROUP BY in CakePHP

Just recently ( end of May, 2008 ) there was an addition to CakePHP’s find method to easily build a GROUP BY in your SQL. If you’ve been using something like $this->Product->find(‘all’… etc., etc. Now you can add a new key ‘group’ to your find method, like so: $this->Product->find(‘all’,array(‘fields’=>array(‘Product.type’,’MIN(Product.price) as[…]

Read More »
%d bloggers like this: