Skip to content

Commit 61e7b36

Browse files
committed
bug #1660 [make:entity] Use stricter DateTime type instead of DateTimeInterface for mutable datetime fields (rosier)
This PR was merged into the 1.x branch. Discussion ---------- [make:entity] Use stricter DateTime type instead of DateTimeInterface for mutable datetime fields The current DateTimeInterface type hint suggest that you can put a DateTimeImmutable in a mutable datetime field, but that is no longer the case. It results in the following error: ``` Could not convert PHP value of type DateTimeImmutable to type Doctrine\DBAL\Types\DateTimeType. Expected one of the following types: null, DateTime. ``` Follows: - doctrine/dbal#6017 - doctrine/dbal#6161 Commits ------- e80aa17 Use stricter DateTime type instead of DateTimeInterface for mutable datetime fields
2 parents 022d79e + e80aa17 commit 61e7b36

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/Doctrine/DoctrineHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public static function getPropertyTypeForColumn(string $columnType): ?string
263263
Types::BOOLEAN => 'bool',
264264
Types::INTEGER, Types::SMALLINT => 'int',
265265
Types::FLOAT => 'float',
266-
Types::DATETIME_MUTABLE, Types::DATETIMETZ_MUTABLE, Types::DATE_MUTABLE, Types::TIME_MUTABLE => '\\'.\DateTimeInterface::class,
266+
Types::DATETIME_MUTABLE, Types::DATETIMETZ_MUTABLE, Types::DATE_MUTABLE, Types::TIME_MUTABLE => '\\'.\DateTime::class,
267267
Types::DATETIME_IMMUTABLE, Types::DATETIMETZ_IMMUTABLE, Types::DATE_IMMUTABLE, Types::TIME_IMMUTABLE => '\\'.\DateTimeImmutable::class,
268268
Types::DATEINTERVAL => '\\'.\DateInterval::class,
269269
'object' => 'object',

tests/Util/fixtures/add_entity_field/User_simple_datetime.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Entity;
44

5-
use Doctrine\DBAL\Types\Types;
65
use Doctrine\ORM\Mapping as ORM;
76

87
#[ORM\Entity]
@@ -13,20 +12,20 @@ class User
1312
#[ORM\Column()]
1413
private ?int $id = null;
1514

16-
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
17-
private ?\DateTimeInterface $createdAt = null;
15+
#[ORM\Column(nullable: true)]
16+
private ?\DateTime $createdAt = null;
1817

1918
public function getId(): ?int
2019
{
2120
return $this->id;
2221
}
2322

24-
public function getCreatedAt(): ?\DateTimeInterface
23+
public function getCreatedAt(): ?\DateTime
2524
{
2625
return $this->createdAt;
2726
}
2827

29-
public function setCreatedAt(?\DateTimeInterface $createdAt): static
28+
public function setCreatedAt(?\DateTime $createdAt): static
3029
{
3130
$this->createdAt = $createdAt;
3231

0 commit comments

Comments
 (0)