PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/en/appendices/4-2-migration-guide.rst

https://github.com/cakephp/docs
ReStructuredText | 216 lines | 160 code | 56 blank | 0 comment | 0 complexity | 86f45d564bc807e7a6332beb59be58cc MD5 | raw file
  1. 4.2 Migration Guide
  2. ###################
  3. CakePHP 4.2 is an API compatible upgrade from 4.0. This page outlines the
  4. deprecations and features added in 4.2.
  5. Upgrading to 4.2.0
  6. ==================
  7. You can use composer to upgrade to CakePHP 4.2.0::
  8. php composer.phar require --update-with-dependencies "cakephp/cakephp:4.2.x"
  9. Deprecations
  10. ============
  11. 4.2 introduces a few deprecations. All of these features will continue for the
  12. duration of 4.x but will be removed in 5.0. You can use the
  13. :ref:`upgrade tool <upgrade-tool-use>` to automate updating usage of deprecated
  14. features::
  15. bin/cake upgrade rector --rules cakephp42 <path/to/app/src>
  16. .. note::
  17. This only updates CakePHP 4.2 changes. Make sure you apply CakePHP 4.1 changes first.
  18. A new configuration option has been added to disable deprecations on a path by
  19. path basis. See :ref:`deprecation-warnings` for more information.
  20. Core
  21. ----
  22. - ``Exception::responseHeader()`` is now deprecated. Users must use ``HttpException::setHeaders()``
  23. when setting HTTP response headers. Application and plugin exceptions that set response headers
  24. should be updated to subclass ``HttpException``.
  25. - ``Cake\Core\Exception\Exception`` was renamed to
  26. ``Cake\Core\Exception\CakeException``.
  27. Database
  28. --------
  29. - ``Cake\Database\Exception`` was renamed to ``Cake\Database\Exception\DatabaseException``.
  30. ORM
  31. ---
  32. - ``TableLocator::allowFallbackClass()`` was added. This method lets you
  33. disable automatically generated fallback table classes. Disabling is
  34. currently opt-in, but will become the default in the future.
  35. - ``ORM\Behavior::getTable()`` has been deprecated. Use ``table()`` instead.
  36. This change makes method names dissimilar between ``ORM\Table`` as the return
  37. value of these methods is different.
  38. Behavior Changes
  39. ================
  40. While the following changes do not change the signature of any methods they do
  41. change the semantics or behavior of methods.
  42. Collection
  43. ----------
  44. - ``Collection::groupBy()`` and ``Collection::indexBy()`` now throw an exception when
  45. the path does not exist or the path contains a null value. Users who need to support
  46. null should use a callback to return a default value instead.
  47. Controller
  48. ----------
  49. - ``Controller::$components`` was marked protected. It was previously documented
  50. as protected. This should not impact most application code as implementations
  51. can change the visibility to public.
  52. Component
  53. ---------
  54. - ``FlashComponent::set()`` now sets the ``element`` option to ``error`` by
  55. default when used with an ``Exception`` instance.
  56. Database
  57. --------
  58. - The ``TimeType`` will now correctly marshall values in the ``H:i`` format.
  59. Previously these values would be cast to ``null`` after validation.
  60. - ``Sqlserver`` driver will retry connect after receiving "Azure Sql Database pausd" errors.
  61. Error
  62. -----
  63. - ``ExceptionRenderer`` now uses the exception code as the HTTP status code
  64. for ``HttpException`` only. Other exceptions that should return a non-500
  65. HTTP code are controlled by ``ExceptionRenderer::$exceptionHttpCodes``.
  66. .. note::
  67. If you need to restore the previous behavior until your exceptions are updated,
  68. you can create a custom ExceptionRenderer and override the ``getHttpCode()`` function.
  69. See :ref:`custom-exceptionrenderer` for more information.
  70. - ``ConsoleErrorHandler`` now uses the exception code as the exit code for
  71. ``ConsoleException`` only.
  72. Validation
  73. ----------
  74. - ``Validation::time()`` will now reject a string if minutes are missing. Previously,
  75. this would accept hours-only digits although the api documentation showed minutes were required.
  76. Breaking Changes
  77. ================
  78. Behind the API, some breaking changes are necessary moving forward.
  79. They usually only affect tests.
  80. I18n
  81. ----
  82. - The dependency on `Aura.Intl <https://github.com/auraphp/Aura.Intl>`_ package has been
  83. removed as it is no longer maintained. If your app/plugin has :ref:`custom translation loaders <creating-generic-translators>`
  84. then they need to now return a ``Cake\I18n\Package`` instance instead of ``Aura\Intl\Package``.
  85. Both the classes are API compatible so you won't need to change anything else.
  86. Testing
  87. -------
  88. - The fixture names around UUIDs have been consolidated (``UuidItemsFixture``, ``BinaryUuidItemsFixture``).
  89. If you use any of them, make sure you updated these names.
  90. The ``UuidportfoliosFixture`` was unused in core and removed now.
  91. New Features
  92. ============
  93. We're adding a new process to enable us to ship features, collect feedback from
  94. the community and evolve those features. We're calling this process
  95. :ref:`experimental-features`.
  96. Core
  97. ----
  98. - Experimental support for a :doc:`/development/dependency-injection` container
  99. was added.
  100. Console
  101. -------
  102. - ``ConsoleIo::comment()`` was added. This method formats text with a blue
  103. foreground like comments in the generated help text.
  104. - ``TableHelper`` now supports a ``<text-right>`` formatting tag, which aligns
  105. cell content with the right edge instead of the left.
  106. Database
  107. --------
  108. - ``SqlServer`` now creates client-side buffered cursors for prepared statements by default.
  109. This was changed to fix significant performance issues with server-side SCROLL cursors.
  110. Users should see a performance boost with most results sets.
  111. .. warning::
  112. For users with large query results, this may cause an error allocating the client-side buffer unless
  113. ``Query::disableBufferedResults()`` is called.
  114. The maximum buffer size can be configured in ``php.ini`` with ``pdo_sqlsrv.client_buffer_max_kb_size``.
  115. See https://docs.microsoft.com/en-us/sql/connect/php/cursor-types-pdo-sqlsrv-driver?view=sql-server-ver15#pdo_sqlsrv-and-client-side-cursors
  116. for more information.
  117. - ``Query::isResultsCastingEnabled()`` was added to get the current result
  118. casting mode.
  119. - ``StringExpression`` was added to use string literals with collation.
  120. - ``IdentifierExpression`` now supports collation.
  121. Http
  122. ----
  123. - ``Cake\Http\Middleware\SessionCsrfProtectionMiddleware`` was added. Instead of
  124. storing CSRF tokens in a cookie, this middleware stores tokens in the session.
  125. This makes CSRF tokens user scoped and time based with the session, offering
  126. enhanced security over cookie based CSRF tokens. This middleware is a drop in
  127. replacement for the ``CsrfProtectionMiddleware``.
  128. - The ``hal+json``, ``hal+xml``, and ``jsonld`` types were added to
  129. ``Response`` making them usable with ``withType()``.
  130. - ``Client::createFromUrl()`` was added. This method can be used to create
  131. HTTP clients scoped to specific domains including a base path.
  132. - A new utility class ``Cake\Http\FlashMessage`` was added whose instance is
  133. available through ``ServerRequest::getFlash()``. The class similar to the
  134. ``FlashComponent`` allows you to set flash messages. It can be particularly
  135. useful for setting flash messages from middlewares.
  136. ORM
  137. ---
  138. - ``Table::subquery()`` and ``Query::subquery()`` were added. These methods
  139. lets you create query objects that don't have automatic aliasing. This helps
  140. reduce overhead and complexity of building subqueries and common table
  141. expressions.
  142. - ``IsUnique`` rule now accepts the ``allowMultipleNulls`` option that was available
  143. in 3.x. This is disabled by default unlike in 3.x.
  144. TestSuite
  145. ---------
  146. - ``EmailTrait::assertMailSubjectContains()`` and
  147. ``assertMailSubjectContainsAt()`` were added.
  148. - ``mockService()`` was added to ``ConsoleIntegrationTestTrait`` and
  149. ``IntegrationTestCaseTrait``. This method enables services injected with the
  150. :doc:`/development/dependency-injection` container to be replaced with mock
  151. or stub objects.
  152. View
  153. ----
  154. - Context classes now include the ``comment``, ``null``, and ``default``
  155. metadata options in the results of ``attributes()``.
  156. - ``ViewBuilder::addHelper()`` now accepts an ``$options`` parameter to pass
  157. options into helper constructors.
  158. - The ``assetUrlClassName`` option was added to ``UrlHelper``. This option lets
  159. you replace the default asset URL resolver with an application specific one.
  160. This can be useful if you need to customize asset cache busting parameters.