Skip to content
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
22 changes: 21 additions & 1 deletion src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable

private InstantiatorInterface|null $instantiator = null;

private readonly TypedFieldMapper $typedFieldMapper;
private TypedFieldMapper $typedFieldMapper;

/**
* Initializes a new ClassMetadata instance that will hold the object-relational mapping
Expand Down Expand Up @@ -824,6 +824,18 @@ public function __sleep(): array
return $serialized;
}

/** @internal */
public function setNamingStrategy(NamingStrategy $namingStrategy): void
{
$this->namingStrategy = $namingStrategy;
}

/** @internal */
public function setTypedFieldMapper(TypedFieldMapper $typedFieldMapper): void
{
$this->typedFieldMapper = $typedFieldMapper;
}

/**
* Creates a new instance of the mapped class, without invoking the constructor.
*/
Expand All @@ -843,6 +855,14 @@ public function wakeupReflection(ReflectionService $reflService): void
$this->reflFields = new LegacyReflectionFields($this, $reflService);
$this->instantiator = $this->instantiator ?: new Instantiator();

if (! isset($this->namingStrategy)) {
$this->namingStrategy = new DefaultNamingStrategy();
}

if (! isset($this->typedFieldMapper)) {
$this->typedFieldMapper = new DefaultTypedFieldMapper();
}

$parentAccessors = [];

foreach ($this->embeddedClasses as $property => $embeddedClass) {
Expand Down
10 changes: 10 additions & 0 deletions src/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,16 @@ private function inheritIdGeneratorMapping(ClassMetadata $class, ClassMetadata $

protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService): void
{
if ($class instanceof ClassMetadata && $this->em !== null) {
$configuration = $this->em->getConfiguration();
$class->setNamingStrategy($configuration->getNamingStrategy());

$typedFieldMapper = $configuration->getTypedFieldMapper();
if ($typedFieldMapper !== null) {
$class->setTypedFieldMapper($typedFieldMapper);
}
}

$class->wakeupReflection($reflService);

if (PHP_VERSION_ID < 80400) {
Expand Down
39 changes: 39 additions & 0 deletions tests/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs;
Expand All @@ -18,13 +19,15 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DefaultTypedFieldMapper;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
use Doctrine\Tests\Mocks\EntityManagerMock;
Expand All @@ -40,12 +43,14 @@
use Doctrine\Tests\Models\Quote\Group;
use Doctrine\Tests\Models\Quote\Phone;
use Doctrine\Tests\Models\Quote\User;
use Doctrine\Tests\Models\TypedProperties\UserTyped;
use Doctrine\Tests\OrmTestCase;
use Doctrine\Tests\TestUtil;
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\Assert;
use ReflectionClass;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

use function array_search;
use function assert;
Expand Down Expand Up @@ -469,6 +474,40 @@ public function testInvalidSubClassCase(): void

$userMetadata = $cmf->getMetadataFor(Shape::class);
}

public function testCachedMetadataRestoresCustomNamingStrategyAndTypedFieldMapper(): void
{
$driver = $this->createStub(MappingDriver::class);
$driver->method('loadMetadataForClass')->willReturnCallback(
static function (string $className, ClassMetadata $metadata): void {
$metadata->mapField(['fieldName' => 'id', 'id' => true]);
},
);

$cache = new ArrayAdapter();
$em = $this->createEntityManager($driver);
$em->getConfiguration()->setMetadataCache($cache);
$em->getConfiguration()->setNamingStrategy(new UnderscoreNamingStrategy());
$em->getConfiguration()->setTypedFieldMapper(new DefaultTypedFieldMapper(['string' => Types::TEXT]));

$cmf1 = new ClassMetadataFactory();
$cmf1->setEntityManager($em);
$cmf1->setCache($cache);
$cmf1->getMetadataFor(UserTyped::class);

$cmf2 = new ClassMetadataFactory();
$cmf2->setEntityManager($em);
$cmf2->setCache($cache);
$metadata = $cmf2->getMetadataFor(UserTyped::class);

$metadata->mapField(['fieldName' => 'dateInterval']);
$metadata->mapField(['fieldName' => 'username']);
$metadata->mapManyToOne(['fieldName' => 'mainEmail']);

self::assertSame('date_interval', $metadata->getColumnName('dateInterval'));
self::assertSame(Types::TEXT, $metadata->getTypeOfField('username'));
self::assertSame('main_email_id', $metadata->getSingleAssociationJoinColumnName('mainEmail'));
}
}

#[Entity]
Expand Down
19 changes: 19 additions & 0 deletions tests/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,25 @@ public function testSetIdentifierWithoutPositionsDefaultsToZero(): void
self::assertSame(['username', 'name'], $cm->identifier);
self::assertSame(['username' => 0, 'name' => 0], $cm->identifierPositions);
}

public function testWakeupReflectionRestoresNamingStrategyAndTypedFieldMapper(): void
{
$cm = new ClassMetadata(UserTyped::class);
$cm->initializeReflection(new RuntimeReflectionService());
$cm->mapField(['fieldName' => 'id', 'id' => true]);

$cm = unserialize(serialize($cm));
assert($cm instanceof ClassMetadata);
$cm->wakeupReflection(new RuntimeReflectionService());

$cm->mapField(['fieldName' => 'username']);
$cm->mapManyToOne(['fieldName' => 'mainEmail']);

self::assertSame(Types::STRING, $cm->getTypeOfField('username'));
self::assertSame('username', $cm->getColumnName('username'));
self::assertSame(CmsEmail::class, $cm->getAssociationTargetClass('mainEmail'));
self::assertSame('mainEmail_id', $cm->getSingleAssociationJoinColumnName('mainEmail'));
}
}

#[MappedSuperclass]
Expand Down
Loading