Skip to content

Commit a86cd70

Browse files
andreaskienastgeorgringer
authored andcommitted
[BUGFIX] Allow DEFAULT NULL for VARCHAR
The database schema generated by `DefaultTcaSchema` lacks the ability to generate `VARCHAR` fields with `NULL` as their default value. The code is adjusted to generate `DEFAULT NULL` for input fields that do not exceed the maximum length of 255 characters. Since this code adjusts existing database schema with potentially negative impact, this patch is aimed for main only. Resolves: #106656 Releases: main Change-Id: Icba421059819ddd6cfdb7bf55c3b80d17dc0b7bc Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/89299 Tested-by: Georg Ringer <georg.ringer@gmail.com> Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de> Tested-by: core-ci <typo3@b13.com> Tested-by: Stefan Bürk <stefan@buerk.tech> Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de> Reviewed-by: Stefan Bürk <stefan@buerk.tech> Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
1 parent 897b104 commit a86cd70

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

typo3/sysext/core/Classes/Database/Schema/DefaultTcaSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ protected function enrichSingleTableFieldsFromTcaColumns(array $tables): array
803803
Types::STRING,
804804
[
805805
'length' => $length,
806-
'default' => '',
806+
'default' => $nullable ? null : '',
807807
'notnull' => !$nullable,
808808
]
809809
);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.. include:: /Includes.rst.txt
2+
3+
.. _important-106656-1746525351:
4+
5+
============================================================
6+
Important: #106656 - Allow `DEFAULT NULL` for varchar fields
7+
============================================================
8+
9+
See :issue:`106656`
10+
11+
Description
12+
===========
13+
14+
In TCA, if an input field is configured to be nullable via
15+
:php:`'nullable' => true`, the database migration now respects this and creates
16+
new or updates existing fields with `DEFAULT NULL`.
17+
18+
In Extbase, this may cause issues if properties and their accessor methods are
19+
not properly declared to be nullable, therefore this change is introduced to
20+
TYPO3 v14 only.
21+
22+
Example:
23+
24+
.. code-block:: php
25+
:caption: Example properly implementing a nullable property
26+
27+
<?php
28+
29+
declare(strict_types=1);
30+
31+
namespace Vendor\myExtension\Domain\Model;
32+
33+
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
34+
35+
class MyExtbaseEntity extends AbstractEntity
36+
{
37+
protected ?string $title;
38+
39+
public function getTitle(): ?string
40+
{
41+
return $this->title;
42+
}
43+
44+
public function setTitle(?string $title): void
45+
{
46+
$this->title = $title;
47+
}
48+
}
49+
50+
As stated above, this automatic detection is not provided in TYPO3 versions
51+
older than 14.0. Using `DEFAULT NULL` can be enforced via an extension's
52+
:file:`ext_tables.sql` instead:
53+
54+
.. code-block:: sql
55+
56+
CREATE TABLE tx_myextension_table (
57+
title varchar(255) DEFAULT NULL
58+
);
59+
60+
.. index:: Database, ext:core

typo3/sysext/core/Tests/Unit/Database/Schema/DefaultTcaSchemaTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1906,7 +1906,7 @@ public function enrichAddsInputAndConsidersNullable(): void
19061906
Type::getType('string'),
19071907
[
19081908
'length' => 255,
1909-
'default' => '',
1909+
'default' => null,
19101910
'notnull' => false,
19111911
]
19121912
);

typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Classes/Domain/Model/Blog.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ public function __construct()
8282
$this->logo = new ObjectStorage();
8383
}
8484

85-
public function getSubtitle(): string
85+
public function setSubtitle(?string $subtitle): void
86+
{
87+
$this->subtitle = $subtitle;
88+
}
89+
90+
public function getSubtitle(): ?string
8691
{
8792
return $this->subtitle;
8893
}
@@ -181,9 +186,4 @@ public function getAdministrator(): Administrator|LazyLoadingProxy|null
181186
{
182187
return $this->administrator;
183188
}
184-
185-
public function setSubtitle(?string $subtitle): void
186-
{
187-
$this->subtitle = $subtitle;
188-
}
189189
}

0 commit comments

Comments
 (0)