Blog

CakePHP

I’ve recently noticed a number of people trying to use CakePHP 2’s updateAll() method very badly.

Let’s first get one thing straight: updateAll() is not the intended way of updating a record in CakePHP, use save() for that!

The last couple of days I’ve been developing a little app that imports Twitter feeds and saves tweets to a MySQL database; however, I quickly hit an issue where certain tweets would not save. MySQL was returning the error message ‘SQLSTATE[HY000]: General error: 1366 Incorrect string value’. Looking at the specific tweets causing the database to throw the error I noticed that they contained emoticons.

This is a kind of follow-up to my recent article on Essential Sublime Text 3 Plugins for PHP Developers to explain how to install and configure Sublime-phpcs for the CakePHP code standard. Although I’m describing how to configure phpcs specifically for Cake it will hopefully be of use to anyone wanting to get started with phpcs in Sublime Text and start developing better code.

The first stable release of CakePHP 3 came out earlier this week so I’ve started the process of updating my Cake plugins. The first one to be updated is VideoEmbed (on the basis that it seems to be the most popular).

Diagram of CakePHP's observer pattern
CakePHP's Observer Pattern (the Events System)

It seems that Events are an often ignored feature of the CakePHP framework. Introduced in version 2.1 the events system provides a means of applying logic that generally doesn’t belong in a model or controller. Developing in Cake is all about its fat models and skinny controllers, but there are some things that belong in neither, and shouldn’t really be placed in a model just to make it fatter!

Sometimes when a user requests to download a file from your app you may want to run some pre-download logic. For example, you may want to check that the user is authenticated or perhaps log the download. Since CakePHP 2.3 this can be simply achieved using CakeResponse.