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

Add automatic type detection for Embedded. #8724

Merged
merged 3 commits into from
May 31, 2021
Merged
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: 1 addition & 1 deletion docs/en/reference/annotations-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ in order to specify that it is an embedded class.

Required attributes:

- **class**: The embeddable class
- **class**: The embeddable class. You can omit this value if you use a PHP property type instead.


.. code-block:: php
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
9 changes: 8 additions & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3593,9 +3593,16 @@ public function mapEmbedded(array $mapping)
{
$this->assertFieldNotMapped($mapping['fieldName']);

if (! isset($mapping['class']) && $this->isTypedProperty($mapping['fieldName'])) {
$type = $this->reflClass->getProperty($mapping['fieldName'])->getType();
if ($type instanceof ReflectionNamedType) {
$mapping['class'] = $type->getName();
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check here again that class isset or otherwise throw an exception. If not, then line 3616 will throw a notice accessing mapping class that does not exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the validation is done "late" in ClassMetadataFactory::doLoadMetadata as per #1133 - this should probably be changed, but not now.

$this->embeddedClasses[$mapping['fieldName']] = [
'class' => $this->fullyQualifiedClassName($mapping['class']),
'columnPrefix' => $mapping['columnPrefix'],
'columnPrefix' => $mapping['columnPrefix'] ?? null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 what does this have to do with the rest of your PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume phpstan failure they mentioned in the PR description.

'declaredField' => $mapping['declaredField'] ?? null,
'originalField' => $mapping['originalField'] ?? null,
];
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)

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

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
7 changes: 2 additions & 5 deletions lib/Doctrine/ORM/Mapping/Embedded.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@
#[Attribute(Attribute::TARGET_PROPERTY)]
final class Embedded implements Annotation
{
/**
* @Required
* @var string
*/
/** @var string|null */
public $class;

/** @var string|bool|null */
public $columnPrefix;

public function __construct(string $class, $columnPrefix = null)
public function __construct(?string $class = null, $columnPrefix = null)
{
$this->class = $class;
$this->columnPrefix = $columnPrefix;
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ parameters:

-
message: "#^Call to an undefined method ReflectionProperty\\:\\:getType\\(\\)\\.$#"
count: 2
count: 3
path: lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php

-
Expand Down
18 changes: 18 additions & 0 deletions tests/Doctrine/Tests/Models/TypedProperties/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\TypedProperties;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Embeddable()
*/
#[ORM\Embeddable]
class Contact
{
/** @Column() */
#[ORM\Column]
public ?string $email = null;
}
7 changes: 7 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 ?Contact $contact = null;

public static function loadMetadata(ClassMetadataInfo $metadata): void
{
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
Expand Down Expand Up @@ -131,5 +136,7 @@ public static function loadMetadata(ClassMetadataInfo $metadata): void
$metadata->mapManyToOne(
['fieldName' => 'mainEmail']
);

$metadata->mapEmbedded(['fieldName' => 'contact']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Doctrine\Tests\Models\DDC889\DDC889Entity;
use Doctrine\Tests\Models\DDC964\DDC964Admin;
use Doctrine\Tests\Models\DDC964\DDC964Guest;
use Doctrine\Tests\Models\TypedProperties\Contact;
use Doctrine\Tests\Models\TypedProperties\UserTyped;
use Doctrine\Tests\OrmTestCase;

Expand Down Expand Up @@ -284,6 +285,7 @@ public function testFieldTypeFromReflection(): void

$this->assertEquals(CmsEmail::class, $class->getAssociationMapping('email')['targetEntity']);
$this->assertEquals(CmsEmail::class, $class->getAssociationMapping('mainEmail')['targetEntity']);
$this->assertEquals(Contact::class, $class->embeddedClasses['contact']['class']);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public function testFieldIsNullableByType(): void

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

$cm->mapEmbedded(['fieldName' => 'contact']);
$this->assertEquals(TypedProperties\Contact::class, $cm->embeddedClasses['contact']['class']);
}

public function testFieldTypeFromReflection(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@
$metadata->mapManyToOne(
['fieldName' => 'mainEmail']
);

$metadata->mapEmbedded(['fieldName' => 'contact']);
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
</one-to-one>

<many-to-one field="mainEmail"/>

<embedded name="contact" />
</entity>
</doctrine-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ Doctrine\Tests\Models\TypedProperties\UserTyped:
joinColumn: []
manyToOne:
mainEmail: []
embedded:
contact: ~