|
| 1 | +5.3 Migration Guide |
| 2 | +################### |
| 3 | + |
| 4 | +The 5.3.0 release is a backwards compatible with 5.0. It adds new functionality |
| 5 | +and introduces new deprecations. Any functionality deprecated in 5.x will be |
| 6 | +removed in 6.0.0. |
| 7 | + |
| 8 | +Upgrade Tool |
| 9 | +============ |
| 10 | + |
| 11 | +The :doc:`upgrade tool </appendices/migration-guides>` provides rector rules for |
| 12 | +automating some of the migration work. Run rector before updating your |
| 13 | +``composer.json`` dependencies:: |
| 14 | + |
| 15 | + bin/cake upgrade rector --rules cakephp53 <path/to/app/src> |
| 16 | + |
| 17 | +Upgrade to PHP 8.2 |
| 18 | +================== |
| 19 | + |
| 20 | +If you are not running on **PHP 8.2 or higher**, you will need to upgrade PHP before updating CakePHP. |
| 21 | + |
| 22 | +.. note:: |
| 23 | + CakePHP 5.3 requires **a minimum of PHP 8.2**. |
| 24 | + |
| 25 | +Behavior Changes |
| 26 | +================ |
| 27 | + |
| 28 | +Core |
| 29 | +---- |
| 30 | + |
| 31 | +- ``InstanceConfigTrait::deleteConfig()`` was added. For classes using this |
| 32 | + trait, you can now use ``$this->deleteConfig('key')`` instead of ``$this->setConfig('key', null)`` |
| 33 | + |
| 34 | +Database |
| 35 | +-------- |
| 36 | + |
| 37 | +- ``Query::with()`` now accepts an array of expressions to align with other query clauses. This also |
| 38 | + allows clearing the expressions with an empty array. |
| 39 | + |
| 40 | +ORM |
| 41 | +--- |
| 42 | + |
| 43 | +- ``joinWith()`` now asserts when the association conflicts with an existing join and will overwrite it. |
| 44 | + Because existing code might harmlessly ignore the join or accidentally rely on that behavior, this change is not breaking |
| 45 | + in production for CakePHP 5. |
| 46 | + |
| 47 | +Validation |
| 48 | +---------- |
| 49 | + |
| 50 | +- The signature of ``Validator::validate(array $data, bool $newRecord = true, array $context = [])`` has now a additional third parameter ``$context``. |
| 51 | + It can be used to pass necessary context into the validation when marshalling. |
| 52 | + |
| 53 | +View |
| 54 | +---- |
| 55 | + |
| 56 | +- The ``format()`` and ``currency()`` methods of ``NumberHelper`` now accept also null as input and can return any default string here. |
| 57 | + This allows for easier templates, in particular baked ones. Make sure to adjust any extending helper (plugin or app level) by adding that type. |
| 58 | + |
| 59 | +Deprecations |
| 60 | +============ |
| 61 | + |
| 62 | +Database |
| 63 | +-------- |
| 64 | +- ``Query::newExpr()`` is deprecated. Use ``Query::expr()`` instead. |
| 65 | + |
| 66 | +Form |
| 67 | +---- |
| 68 | + |
| 69 | +- ``Form::_execute()`` is deprecated. You should rename your ``_execute`` |
| 70 | + methods to ``process()`` which accepts the same parameters and has the same |
| 71 | + return type. |
| 72 | + |
| 73 | +Http |
| 74 | +---- |
| 75 | + |
| 76 | +- Using ``$request->getParam('?')`` to get the query params is deprecated. |
| 77 | + Use ``$request->getQueryParams()`` instead. |
| 78 | + |
| 79 | +ORM |
| 80 | +--- |
| 81 | + |
| 82 | +- Calling behavior methods on table instances is now deprecated. To call |
| 83 | + a method of an attached behavior you need to use |
| 84 | + ``$table->getBehavior('Sluggable')->slugify()`` instead of ``$table->slugify()``. |
| 85 | +- ``EntityTrait::isEmpty()`` is deprecated. Use ``hasValue()`` instead. |
| 86 | + |
| 87 | +Plugin |
| 88 | +------ |
| 89 | + |
| 90 | +- Loading of plugins without a plugin class is deprecated. For your existing plugins |
| 91 | + which don't have one, you can use the ``bin/cake bake plugin MyPlugin --class-only`` |
| 92 | + command, which will create the file ``plugins/MyPlugin/src/MyPlugin.php``. |
| 93 | + |
| 94 | +View |
| 95 | +---- |
| 96 | + |
| 97 | +- Passing an array as the first argument to ``BreadcrumbsHelper::add()`` and |
| 98 | + ``BreadcrumbsHelper::prepend()`` is deprecated. Use ``addMany()`` and |
| 99 | + ``prependMany()`` instead. |
| 100 | + |
| 101 | +New Features |
| 102 | +============ |
| 103 | + |
| 104 | +Cache |
| 105 | +----- |
| 106 | + |
| 107 | +- Added Redis Cluster support to ``RedisEngine``. Configure the ``cluster`` option |
| 108 | + with an array of server addresses to enable cluster mode. |
| 109 | +- Several :ref:`cache-events` were added to allow monitoring the caching behavior. |
| 110 | + |
| 111 | +Collection |
| 112 | +---------- |
| 113 | + |
| 114 | +- ``Collection::any()`` was added to replace ``Collection::some()`` with a more familiar name. |
| 115 | + |
| 116 | +Command |
| 117 | +------- |
| 118 | + |
| 119 | +- ``cake plugin assets symlink`` command now supports a ``--relative`` option to |
| 120 | + create relative path symlinks. This is useful when creating symlinks within |
| 121 | + containers that use volume mounts. |
| 122 | +- ``cake server`` now supports a ``--frankenphp`` option that will start the |
| 123 | + development server with `FrankenPHP <https://frankenphp.dev/>`__. |
| 124 | + |
| 125 | +Console |
| 126 | +------- |
| 127 | + |
| 128 | +- Added ``TreeHelper`` which outputs an array as a tree such as an array of filesystem |
| 129 | + directories as array keys and files as lists under each directory. |
| 130 | +- Commands can now implement ``getGroup()`` to customize how commands are |
| 131 | + grouped in ``bin/cake -h`` output. |
| 132 | +- ``CommandCollection::replace()`` was added. This method allows you to replace |
| 133 | + an existing command in the collection without needing to remove and re-add it. |
| 134 | + This is particularly useful when using ``autoDiscover`` and you want to replace |
| 135 | + a command with a customized version. |
| 136 | + |
| 137 | +Core |
| 138 | +---- |
| 139 | + |
| 140 | +- Added ``Configure`` attribute to support injecting ``Configure`` values into |
| 141 | + constructor arguments. See ref:`configure-dependency-injection`. |
| 142 | + |
| 143 | +Database |
| 144 | +-------- |
| 145 | + |
| 146 | +- Added support for Entra authentication to SqlServer driver. |
| 147 | +- Added ``Query::optimizerHint()`` which accepts engine-specific optimizer hints. |
| 148 | +- Added ``Query::getDriver()`` helper which returns the ``Driver`` for the current connection |
| 149 | + role by default. |
| 150 | +- Added support for ``year`` column types in MySQL. |
| 151 | +- Added support for ``inet``, ``cidr`` and ``macaddr`` network column types to |
| 152 | + postgres driver. |
| 153 | +- Added ``TypeFactory::getMapped()`` to retrieve the mapped class name for a specific type. |
| 154 | + This provides a cleaner API compared to using ``TypeFactory::getMap()`` with a type argument. |
| 155 | + |
| 156 | +Error |
| 157 | +----- |
| 158 | + |
| 159 | +- ``Debugger`` now replaces :php:const:``ROOT` with ` the |
| 160 | + ``Debugger.editorBasePath`` Configure value if defined. This improves |
| 161 | + debugging workflows within containerized environments. |
| 162 | + |
| 163 | +Http |
| 164 | +---- |
| 165 | + |
| 166 | +- The new ``RateLimitMiddleware`` provides configurable rate limiting for your |
| 167 | + application to protect against abuse and ensure fair usage of resources. It |
| 168 | + supports multiple identification strategies (IP, user, route, API key), |
| 169 | + different rate limiting algorithms (sliding window, fixed window, token bucket), |
| 170 | + and advanced features like custom identifiers, request costs, and dynamic limits. |
| 171 | +- ``UnprocessableContentException`` was added. |
| 172 | + |
| 173 | +I18n |
| 174 | +---- |
| 175 | + |
| 176 | +- Added ``DateTimePeriod`` which wraps a php ``DatePeriod`` and returns ``DateTime`` |
| 177 | + instances when iterating. |
| 178 | +- Added ``DatePeriod`` which wraps a php ``DatePeriod`` and returns ``Date`` instances |
| 179 | + when iterating. |
| 180 | +- Added ``toQuarterRange()`` method to ``DateTime`` and ``FrozenTime`` classes which returns |
| 181 | + an array containing the start and end dates of the quarter for the given date. |
| 182 | +- Added ``Date::getTimestamp()``. This method returns an int of the date's |
| 183 | + timestamp. |
| 184 | + |
| 185 | +Mailer |
| 186 | +------ |
| 187 | + |
| 188 | +- Added ``Message::addAttachment()`` for adding attachments to a message. Like |
| 189 | + other message methods, it can be accessed via the ``Mailer`` instance as ``$mailer->addAttachment()``. |
| 190 | + |
| 191 | +ORM |
| 192 | +--- |
| 193 | + |
| 194 | +- ``Table::patchEntity()``, ``Table::newEntity()``, ``Marshaller::one()`` and |
| 195 | + ``Marshaller::many()`` now accept a ``strictFields`` option that only applies |
| 196 | + validation to the fields listed in the ``fields`` option. |
| 197 | +- Added ``TableContainer`` that you can register in your ``Application::services()`` to |
| 198 | + add dependency injection for your Tables. |
| 199 | +- Added ``SelectQuery::projectAs()`` for projecting query results into Data |
| 200 | + Transfer Objects (DTOs) instead of Entity objects. DTOs provide a |
| 201 | + memory-efficient alternative (approximately 3x less memory than entities) for |
| 202 | + read-only data access. See :ref:`dto-projection`. |
| 203 | +- Added the ``#[CollectionOf]`` attribute for declaring the element type of |
| 204 | + array properties in DTOs. This enables proper hydration of nested |
| 205 | + associations into DTOs. |
| 206 | + |
| 207 | +Pagination |
| 208 | +---------- |
| 209 | + |
| 210 | +- Added ``SortableFieldsBuilder`` class enabling fluent configuration of |
| 211 | + sortable fields with advanced features. The ``sortableFields`` option now |
| 212 | + accepts a callable that receives a ``SortableFieldsBuilder`` instance, |
| 213 | + allowing you to map friendly sort keys to database fields with multi-column |
| 214 | + sorting and direction control. |
| 215 | +- Added ``SortField`` class for defining sort field configurations with |
| 216 | + customizable default directions and locked directions (e.g., |
| 217 | + ``SortField::asc('price')`` or ``SortField::desc('created', locked: true)``). |
| 218 | +- Added support for combined sorting keys in URLs (e.g., ``?sort=title-asc`` or |
| 219 | + ``?sort=price-desc``) in addition to the traditional ``?sort=field&direction=asc`` |
| 220 | + format. |
| 221 | + |
| 222 | +Routing |
| 223 | +------- |
| 224 | + |
| 225 | +- Added ``RouteBuilder::setOptions()`` method to set default route options at |
| 226 | + the scope level. This allows you to apply options like ``_host``, ``_https``, |
| 227 | + and ``_port`` to all routes within a scope without repeating them on each |
| 228 | + route. Options set at the scope level are inherited by nested scopes and can |
| 229 | + be overridden on individual routes. |
| 230 | +- ``EntityRoute`` now handles enum value conversions. This enables you to use |
| 231 | + enum backed properties as route parameters. When an enum backed property is |
| 232 | + used in routing, the enum's ``value`` or ``name`` will be used. |
| 233 | +- Added ``RedirectTrait``. This trait can be used to create custom redirect route |
| 234 | + classes. |
| 235 | + |
| 236 | +TestSuite |
| 237 | +--------- |
| 238 | + |
| 239 | +- ``assertRedirectBack()`` added to assert a successful redirect has been made to the same previous URL. |
| 240 | +- ``assertRedirectBackToReferer()`` added to assert a successful redirect has been made to the referer URL. |
| 241 | +- ``assertFlashMessageContains()`` and ``assertFlashMessageContainsAt()`` were added. These methods enable |
| 242 | + substring matching of flash message content. |
| 243 | +- ``TestFixture::$tableAlias`` was added. This property lets you define the |
| 244 | + table alias that will be used to load an ``ORM\Table`` instance for a fixture. |
| 245 | + This improves compatibility for schemas that do not closely follow naming conventions. |
| 246 | + |
| 247 | +Utility |
| 248 | +------- |
| 249 | + |
| 250 | +- ``Text::uuid()`` now supports configurable UUID generation. You can set a custom |
| 251 | + UUID generator using ``Configure::write('Text.uuidGenerator', $closure)`` to |
| 252 | + integrate your own UUID generation strategy or third-party libraries. |
| 253 | + |
| 254 | +Validation |
| 255 | +---------- |
| 256 | + |
| 257 | +- ``ipOrRange()`` validation has has been added to check for an IP or a range (subnet). |
| 258 | +- When validating within CakePHP marshalling context, the entity will be passed |
| 259 | + into the ``context`` argument for use inside custom validation rules. This can |
| 260 | + be useful when patching partially and then needing to get that data from the |
| 261 | + entity instead of the passed data. |
| 262 | +- ``existsInNullable()`` rule has been added. This rule allows ``null`` values |
| 263 | + in nullable composite foreign keys, which is semantically correct for optional |
| 264 | + relationships. Use ``$rules->existsInNullable(['author_id', 'site_id'], |
| 265 | + 'SiteAuthors')`` instead of ``existsIn()`` when you want to permit null values |
| 266 | + in foreign keys. |
| 267 | + |
| 268 | +View |
| 269 | +---- |
| 270 | + |
| 271 | +- :php:meth:`HtmlHelper::scriptStart()` and ``scriptEnd()`` now allow simple |
| 272 | + wrapping script tags (``<script>...</script>``) around inline JavaScript. This |
| 273 | + enables syntax highlighting in many editors to work. The wrapping script tag |
| 274 | + will be removed and replaced with a script tag generated by the helper. |
| 275 | + |
| 276 | +- :php:class:`FormHelper` now supports a new option ``nestedCheckboxAndRadio``. |
| 277 | + By default, the helper generates inputs of type checkbox and radio nested |
| 278 | + inside their label. Setting the ``nestedCheckboxAndRadio`` option to ``false`` |
| 279 | + will turn off the nesting. |
| 280 | + |
| 281 | +- ``ViewBuilder::setConfigMergeStrategy()`` was added to control how view options |
| 282 | + are merged with the View class's default configuration. Available strategies are |
| 283 | + ``ViewBuilder::MERGE_DEEP`` (recursive merge, default) and ``ViewBuilder::MERGE_SHALLOW`` |
| 284 | + (simple array merge). This is useful when you want to replace array values in view |
| 285 | + options rather than deep merging them. |
| 286 | + |
| 287 | +- ``ViewBuilder::getConfigMergeStrategy()`` was added to retrieve the current merge |
| 288 | + strategy setting. |
| 289 | + |
| 290 | +- :php:meth:`PaginatorHelper::limitControl()` now automatically respects the |
| 291 | + ``maxLimit`` configuration from the paginator, filtering out any limit options |
| 292 | + that exceed it. A new ``steps`` option was added to automatically generate limit |
| 293 | + options in multiples of a specific value (e.g., ``['steps' => 10]`` generates |
| 294 | + 10, 20, 30... up to maxLimit). |
| 295 | + |
| 296 | +- :php:meth:`BreadcrumbsHelper::addMany()` and :php:meth:`BreadcrumbsHelper::prependMany()` |
| 297 | + were added. These methods allow adding multiple breadcrumbs at once with support |
| 298 | + for shared options that apply to all crumbs. |
0 commit comments