Validation
Let us consider “validation” in a little more detail to see how it has been implemented and optimized in CakePHP 3.0. In addition to what we discussed in the earlier sections, validation now incorporates two complementary conceptions or areas. These include 1) data type and format validation and 2) Application rules.
1. Data Type and Format Validation
This part of the validation deals structural aspects such as data type, format validation, and basic types. Unlike in previous versions, validation is applied before ORM entities are created. This is a very useful feature that ensures everything is totally in sync and set in a way that preserves data integrity and the overall stability of the entire application. Moreover, it markedly reduces application errors and inconsistencies throughout the system. It is therefore a significant enhancement over previous versions.
2. Application Rules
Application rules are the second component of validation in CakePHP 3.0 implementation. They play a key role in quality control to ensure that all application rules and workflows are operating in an orderly and systematic fashion. This is implemented through buildRules() method in tables. Here is a code example that uses buildRules() method for articles table.
// In src/Model/Table/ArticlesTable.php namespace App\Model\Table; use Cake\ORM\Table; use Cake\ORM\RulesChecker; class Articles extends Table { public function buildRules(RulesChecker $rules) { $rules->add($rules->existsIn('user_id', 'Users')); $rules->add( function ($article, $options) { return ($article->published && empty($article->reviewer)); }, 'isReviewed', [ 'errorField' => 'published', 'message' => 'Articles must be reviewed before publishing.' ] ); return $rules; } }
Identifier Quoting
Identifier quoting is another CakePHP feature or process that has changed in CakePHP 3.0. In the new release, quoted identifiers, which were expensive and involved a notoriously error-prone process of parsing SQL snippets has been disabled by default - thereby removing a major source of frustration for developers. The only time you may want to enable identifier quoting is when working with column names or table names with special characters or reserved words. Here is how to enable identifier quoting when configuring a connection.
// In config/app.php 'Datasources' => [ 'default' => [ 'className' => 'Cake\Database\Driver\Mysql', 'username' => 'root', 'password' => 'super_secret', 'host' => 'localhost', 'database' => 'cakephp', 'quoteIdentifiers' => true ] ],
Note: Identifiers in QueryExpression objects require manual quoting or IdentifierExpression objects.
Updating Behaviors
Let us now turn to behaviors. As with most features that has to do with ORM, the way behaviors are setup and configured has evolved for smooth integration with the new framework. Among other things, behaviors now attach to table instances. Here are some other significant differences in the way behaviors are handled in CakePHP as compared to earlier versions.
1. Each table that uses a behavior will have its own instance. No storing of “name space” setting in a behavior is required.
2. Method signature for mixin, callback, and base class for behaviors have all changed
3. Finder methods can now be added easily by behaviors.
The above, in a nutshell, summarizes the main changes and enhancements in the new ORM and CakePHP 3.0 in general. Like all major releases or upgrades, the new release supplants many processes and functions in previous versions while at the same time adding many brand new features.
But as you go through the initial learning curve, please remember that you, the developer, have been the primary driving force behind the changes and enhancements. Your feedback and critiques over the years was the invaluable source that inspired CakePHP team to produce this groundbreaking and cutting-edge release that you are reviewing.