This chapter deals with two aspects of CakePHP models that are fundamental to most applications: validation, and behaviors.
When we are saving information to a data source, such as a database, CakePHP will automatically ensure that the data is quoted in order to prevent attacks, SQL injection being the most common one. If we also need to ensure that the data follows a certain format, for example, that a phone number is valid, we use validation rules.
There are also times where we need to do more than just validate the data we are working with. In some cases, we need to set values for fields that the end user can't specify but are part of our application logic. CakePHP's behaviors allow us to extend the functionality provided by a model, using callbacks to manipulate the data before it's saved, or after it's fetched.
The third recipe shows us how to use model callbacks (such as beforeFind
and afterFind
) in behaviors, while the fourth recipe shows how to use behaviors to add additional...