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

Resolve embeded class by property type #8729

Closed
wants to merge 5 commits into from
Closed
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
8 changes: 4 additions & 4 deletions docs/en/reference/annotations-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ annotation to establish the relationship between the two classes.
The embedded annotation is required on an entity's member variable,
in order to specify that it is an embedded class.

Required attributes:
Optional attributes:

- **class**: The embeddable class
- **class**: The embeddable class, will be resolved automatically when using typed properties


.. code-block:: php
Expand All @@ -361,9 +361,9 @@ Required attributes:
class User
{
/**
* @Embedded(class = "Address")
* @Embedded()
*/
private $address;
private Address $address;

/**
* @Embeddable
Expand Down
2 changes: 1 addition & 1 deletion doctrine-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@

<xs:complexType name="embedded">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="class" type="orm:fqcn" use="required" />
<xs:attribute name="class" type="orm:fqcn" use="optional" />
<xs:attribute name="column-prefix" type="xs:string" use="optional" />
<xs:attribute name="use-column-prefix" type="xs:boolean" default="true" use="optional" />
</xs:complexType>
Expand Down
20 changes: 20 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3605,6 +3605,10 @@ public function mapEmbedded(array $mapping)
{
$this->assertFieldNotMapped($mapping['fieldName']);

if ($this->isTypedProperty($mapping['fieldName']) && ! $mapping['class']) {
$mapping = $this->validateAndCompleteEmbeddedMapping($mapping);
}

$this->embeddedClasses[$mapping['fieldName']] = [
'class' => $this->fullyQualifiedClassName($mapping['class']),
'columnPrefix' => $mapping['columnPrefix'],
Expand All @@ -3613,6 +3617,22 @@ public function mapEmbedded(array $mapping)
];
}

/**
* @param array<string, mixed> $mapping
*
* @return array<string, mixed>
*/
private function validateAndCompleteEmbeddedMapping(array $mapping): array
{
$type = $this->reflClass->getProperty($mapping['fieldName'])->getType();

if ($type instanceof ReflectionNamedType && class_exists($type->getName())) {
$mapping['class'] = $type->getName();
}

return $mapping;
}

/**
* Inline the embeddable class
*
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,13 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)

$mapping = [
'fieldName' => (string) $embeddedMapping['name'],
'class' => (string) $embeddedMapping['class'],
'columnPrefix' => $useColumnPrefix ? $columnPrefix : false,
];

if (isset($embeddedMapping['class'])) {
$mapping['class'] = (string) $embeddedMapping['class'];
}

$metadata->mapEmbedded($mapping);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
foreach ($element['embedded'] as $name => $embeddedMapping) {
$mapping = [
'fieldName' => $name,
'class' => $embeddedMapping['class'],
'class' => $embeddedMapping['class'] ?? null,
'columnPrefix' => $embeddedMapping['columnPrefix'] ?? null,
];
$metadata->mapEmbedded($mapping);
Expand Down
22 changes: 22 additions & 0 deletions tests/Doctrine/Tests/Models/TypedProperties/EmbeddableEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\TypedProperties;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Embeddable
*/
#[ORM\Embeddable]
class EmbeddableEntity
{
/**
* @ORM\Id
* @ORM\Column
* @ORM\GeneratedValue
*/
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
public int $id;
}
5 changes: 5 additions & 0 deletions tests/Doctrine/Tests/Models/TypedProperties/UserTyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class UserTyped
*/
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
public int $id;

/** @Column(length=50) */
#[ORM\Column(length: 50)]
public ?string $status;
Expand Down Expand Up @@ -67,6 +68,10 @@ class UserTyped
#[ORM\ManyToOne]
public ?CmsEmail $mainEmail;

/** @Embedded */
#[ORM\Embedded]
public EmbeddableEntity $embeddableField;

public static function loadMetadata(ClassMetadataInfo $metadata): void
{
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
Expand Down
7 changes: 7 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public function testFieldIsNullableByType(): void

$cm->mapManyToOne(['fieldName' => 'mainEmail']);
$this->assertEquals(CmsEmail::class, $cm->getAssociationMapping('mainEmail')['targetEntity']);

$cm->mapEmbedded([
'fieldName' => 'embeddableField',
'class' => null,
'columnPrefix' => null,
]);
$this->assertEquals(TypedProperties\EmbeddableEntity::class, $cm->embeddedClasses['embeddableField']['class']);
}

public function testFieldTypeFromReflection(): void
Expand Down