What Is New in CakePHP 3.4
CakePHP 3.4 delivers incremental improvements and deprecations that prepare the codebase for future major releases. The focus is on refining existing components and enhancing developer experience with more flexible tools.
| Category | Key Changes |
|---|---|
| New Features | New hasMany('Through') association type, FormHelper template system overhaul. |
| Improvements | Enhanced Collection methods, better session handling, improved fixture loading. |
| Deprecated | Several methods in TableRegistry, Network\Session, and static methods in Log\Log. |
| Bug Fixes | Various fixes across ORM, Database, and View layers. |
How Are Database Associations More Powerful Now?
The ORM now supports a hasMany('Through') association. This is useful for modeling relationships where a join table contains additional data or metadata, bridging two distant tables.
In practice, this works similarly to hasOne('Through') but returns multiple related entities. You define it in your table class's initialize method. This matters because it simplifies complex data models that previously required custom finder logic or intermediate associations.
// Example: A User hasMany Articles through UserArticles
$this->hasMany('Articles', [
'through' => 'UserArticles',
]);
What Changed in the FormHelper Template System?
The FormHelper received a significant internal refactor for its template system. The new structure provides clearer separation between form elements, labels, and error messages, making customization more intuitive.
Instead of a single complex template string, you now work with a template array. This gives you finer control over the HTML wrapper for each part of a form input. The old input template is split into label, input, error, and formGroup templates.
// New template array structure
$_defaultWidgetTemplates = [
'label' => '<label{{attrs}}>{{text}}</label>',
'input' => '<input{{attrs}}/>',
'error' => '<div class="error-message"{{attrs}}>{{content}}</div>'
];
Which Parts of the Framework Are Now Deprecated?
Several components are marked for removal in CakePHP 4.0. The deprecation notices help you update your code ahead of the breaking changes.
TableRegistry Static Methods
Static methods like TableRegistry::config() and TableRegistry::clear() are deprecated. Use the instance methods on the TableRegistry object instead.
Network\Session
The entire Network\Session class is deprecated. Switch to using Cake\Http\Session.
Log\Log Static Methods
Static methods on Log\Log like Log::config() are deprecated. Use Log::setConfig() and other instance methods.
What Collection Utilities Were Added?
The Collection class gained new methods for more expressive data manipulation. These include unfold(), through(), and insert().
unfold() is handy for flattening a collection of arrays or iterators into a single sequence. through() allows you to pipe the collection through a callable, useful for creating custom chainable operations.
$items = $collection->unfold(function ($item) {
return $item->children;
});
$result = $collection->through(function ($items) {
return $items->filter($someFilter);
});
FAQ
Why was the hasMany('Through') association added?
It addresses a gap in modeling complex relationships where a join table holds extra data and you need a one-to-many connection. Before this, developers had to build workarounds using multiple associations or custom finders.
How do I migrate my custom FormHelper templates to 3.4?
You need to split your old monolithic template string into the new array structure. Check your templates for input, label, error, and formGroup parts and define them separately in the template array.
What should I use instead of TableRegistry::config()?
Get an instance of the TableRegistry first, then call instance methods. For example, use TableRegistry::getTableLocator()->config() instead of the static call.
Is the session deprecation going to break my existing code immediately?
No, it triggers deprecation warnings in your logs but continues to work in 3.4. You should plan to replace Network\Session with Cake\Http\Session before upgrading to CakePHP 4.x.
What's the practical use of the new Collection::through() method?
It lets you inject a custom transformation into a chain of collection methods. This is useful for debugging, logging intermediate results, or applying a reusable operation without breaking method chaining.