Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Timestampable] Prevent deprecated ArrayAccess on FieldMapping in doctrine/orm 3 #2834

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ a release.
---

## [Unreleased]
### Fixed
- Prevent depected ArrayAccess on FieldMapping in Timestampable ORM with `doctrine/orm` 3

## [3.16.1]
### Fixed
Expand Down
17 changes: 15 additions & 2 deletions src/Timestampable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function setClock(ClockInterface $clock): void
public function getDateValue($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
$converter = Type::getType($mapping['type'] ?? Types::DATETIME_MUTABLE);
$converter = Type::getType($this->getType($mapping) ?? Types::DATETIME_MUTABLE);
$platform = $this->getObjectManager()->getConnection()->getDriver()->getDatabasePlatform();

return $converter->convertToPHPValue($this->getRawDateValue($mapping), $platform);
Expand All @@ -58,7 +58,7 @@ public function getDateValue($meta, $field)
private function getRawDateValue($mapping)
{
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? '');
$type = $this->getType($mapping) ?? '';

if ('integer' === $type) {
return (int) $datetime->format('U');
Expand All @@ -70,4 +70,17 @@ private function getRawDateValue($mapping)

return \DateTime::createFromImmutable($datetime);
}

/**
* Extract the type from the given FieldMapping.
* Supports both, doctrine/orm 2 and 3 and can be replaces with $mapping->type after doctrine/orm 2 support was dropped.
*/
private function getType(FieldMapping|array $mapping): string|null
{
if(! $mapping instanceof FieldMapping) {
return $mapping['type'] ?? null;
}

return $mapping->type;
}
}