Skip to content

Commit a0be9cb

Browse files
linawolffroemken
andauthored
[TASK] Explain default values for Extbase properties (#5516)
* [TASK] Explain default values for Extbase properties Releases: main, 13.4, 12.4 * [TASK] Explain default values for Extbase properties Releases: main, 13.4, 12.4 * [TASK] Explain default values for Extbase properties Releases: main, 13.4, 12.4 * Update Documentation/ExtensionArchitecture/Extbase/Reference/Domain/Model.rst --------- Co-authored-by: Stefan Frömken <[email protected]>
1 parent 18d863f commit a0be9cb

File tree

1 file changed

+82
-0
lines changed
  • Documentation/ExtensionArchitecture/Extbase/Reference/Domain

1 file changed

+82
-0
lines changed

Documentation/ExtensionArchitecture/Extbase/Reference/Domain/Model.rst

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ In the TYPO3 backend models are displayed as :ref:`database-records`.
2323

2424
.. include:: /CodeSnippets/Extbase/Domain/AbstractEntity.rst.txt
2525

26+
.. warning::
27+
Extbase does not call the constructor when thawing objects. Therefore you
28+
cannot set default values or initialize properties in the constructor.
29+
This includes properties that are defined via constructor parameter
30+
promotion. See also `Default values for model properties <https://docs.typo3.org/permalink/t3coreapi:extbase-model-properties-default-values>`_.
31+
2632
.. contents:: Table of content
2733
:local:
2834

@@ -77,6 +83,82 @@ however get displayed when explicitly called:
7783
But it is there:
7884
<f:debug>{post.info.combinedString}</f:debug>
7985

86+
.. _extbase-model-properties-default-values:
87+
88+
Default values for model properties
89+
-----------------------------------
90+
91+
When Extbase loads an object from the database, it does **not** call the
92+
constructor.
93+
94+
This is explained in more detail in the section
95+
`thawing objects of Extbase models <https://docs.typo3.org/permalink/t3coreapi:extbase-model-hydrating>`_.
96+
97+
This means:
98+
99+
- Property promotion in the constructor (for example
100+
:php:`__construct(public string $title = '')`) **does not work**
101+
- Properties must be initialized in a different way to avoid runtime errors
102+
103+
.. _extbase-model-properties-default-values-directly:
104+
105+
Good: Set default values directly
106+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107+
108+
You can assign default values when defining the property. This works for simple
109+
types such as strings, integers, booleans or nullable properties:
110+
111+
.. code-block:: php
112+
:caption: EXT:my_extension/Classes/Domain/Model/Blog.php
113+
114+
class Blog extends AbstractEntity
115+
{
116+
protected string $title = '';
117+
protected ?\DateTime $modified = null;
118+
}
119+
120+
.. _extbase-model-properties-default-values-initialize:
121+
122+
Good: Use ``initializeObject()`` for setup
123+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124+
125+
If a property needs special setup (for example, using `new ObjectStorage()`),
126+
you can put that logic into a method called `initializeObject()`. Extbase
127+
calls this method automatically after loading the object:
128+
129+
.. literalinclude:: _Hydrating/_Blog3.php
130+
:caption: EXT:my_extension/Classes/Domain/Model/Blog.php
131+
132+
.. _extbase-model-properties-default-values-cpp:
133+
134+
Avoid: Constructor property promotion
135+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136+
137+
This will **not** work when the object comes from the database:
138+
139+
.. code-block:: php
140+
141+
public function __construct(protected string $title = '') {}
142+
143+
Since the constructor is never called during hydration, such properties remain
144+
uninitialized and can cause errors like:
145+
146+
Error: Typed property MyVendor\MyExtension\Domain\Model\Blog::$title
147+
must not be accessed before initialization
148+
149+
To prevent this, always initialize properties either where they are defined or
150+
inside the `initializeObject()` method.
151+
152+
.. _extbase-model-properties-default-values-tca:
153+
154+
TCA default values
155+
~~~~~~~~~~~~~~~~~~
156+
157+
If the TCA configuration of a field defines a
158+
:ref:`default value <t3tca:confval-input-default>`, that value is applied **after**
159+
`initializeObject()` has been called, and **before** data from the database is
160+
mapped to the object.
161+
80162
.. _extbase-model-properties-union-types:
81163

82164
Union types of Extbase model properties

0 commit comments

Comments
 (0)