Category Archives: tip

Getting Things Read

As a blog reader you probably know the “problem” of the daily information flood from the blogosphere. If you have subscribed dozens of feeds you usually get dozens of new posts a day. In this post I will show you how I deal with that “problem”. My strategy consists of two elements: scanning phases and […]

Don’t forget to exit after a redirect

Last weekend I discovered something I was not aware that it works that way: that code defined after a redirect is executed. A simple example: class UsersController extends AppController { function beforeFilter() { // not logged in, so redirect to the login page $this->redirect(‘/login’); } function delete($id) { $this->User->delete($id); } } This example redirects _and_ […]

Selecting data in a specific date range

Sometimes, you have to return all objects created in a specific date range, e.g. you want to return all posts created in the last thirty days. Thanks to the nice functions for date operations provided by MySQL this task is simple: $this->Post->findAll( ‘WHERE Post.created >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)’);

Take over the “control” of some urls from CakePHP

By default, CakePHP takes full “control” of all URLs at the point where it is installed. Sometimes that behaviour is not desired. For example, my hoster defines an url like where the statistics are available. If I install CakePHP in the root, this url no longer works resp. causes an error in CakePHP. The […]

How to use multiple AppModels?

CakePHP supports just one custom AppModel, which must be placed in the /app folder. If you place another AppModel in the app folder and you try to extend your model from that AppModel, you get a “Class not found” error. To avoid that error you simply have to add a require statement at the top […]

How to apply a beforeSave to certain models only

This post is the answer to a question asked by Ian Hill in a comment to my previous post: Is there a similar way to stop custom functions (especially beforeSave()) from being called in your model? I hope I understood the question correctly and the following example answers the question ;-) // app/app_model.php class AppModel […]

How to apply a beforeFilter to certain actions only

If you define a beforeFilter in your controller, it will be executed before each action. Sometimes, that behaviour is not desired, and you want to exclude an action from the application of the beforeFilter. The following simple example gives you an idea of how to accomplish that: // app/controllers/users_controller.php class UsersController extends AppController { function […]

Be careful with file uploads

In a comment lamby pointed out a possible security hole in the code of my post “File upload with CakePHP”. He is right, if you know the location of app/config/database.php you can retrieve the database settings including the password. In the following I will show you the original code, the exploit, and a possible solution. […]

Using bake with (PHP)Eclipse

Lately, Matt posted in a thread with the title “cool editors for using with cakephp” a tip for using bake from within PHPEclipse: Choose “Run”, “External Tools”, “External Tools…” Select “Program” and create a new configuration with “New” There are two ways to configure such a configuration: Location: /usr/bin/php Working Directory: <where_cake_is>/cake/scripts Arguments: bake.php Or […]

How to update multiple divs with Ajax

This is a question which arises from time to time in the CakePHP google group. There is an example in the group, but I have to admit I didn’t understood it the first time I read it. So I try to provide a better example. First we create a view with an Ajax link and […]