Skip to content

[DoctrineBridge] UniqueEntity does no longer access nested property #57279

@ker0x

Description

@ker0x

Symfony version(s) affected

7.1.0

Description

When validating an entity with embeddable properties, UniqueEntity constraint no longer access embedded properties and throw an exception.

This was working fine prior to 7.1

How to reproduce

Create an entity with embeddable and defined an embedded property in the property fields of the UniqueEntity

<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\EstablishmentRepository;
use App\Entity\Embeddable\Location;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as Orm;
use Symfony\Bridge\Doctrine\Validator\Constraints;

#[Orm\Table(name: 'establishment')]
#[Orm\Entity(repositoryClass: EstablishmentRepository::class)]
#[UniqueEntity(fields: ['name', 'location.address.postalCode', 'location.address.city'], message: 'establishment.name.unique')]
class Establishment
{
    #[Orm\Column(type: Types::STRING, length: 255, unique: true)]
    private ?string $name = null;

    #[Orm\Embedded(class: Location::class)]
    private Location $location;

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): static
    {
        $this->name = $name;

        return $this;
    }

    public function getLocation(): LocationInterface
    {
        return $this->location;
    }

    public function setLocation(LocationInterface $location): static
    {
        $this->location = $location;

        return $this;
    }
}
<?php

declare(strict_types=1);

namespace App\Entity\Embeddable;

use Doctrine\ORM\Mapping as Orm;

#[Orm\Embeddable]
class Location
{
    #[Orm\Embedded(class: Address::class)]
    private Address $address;

    public function __construct()
    {
        $this->address = new Address();
    }

    public function getAddress(): Address
    {
        return $this->address;
    }

    public function setAddress(Address $address): static
    {
        $this->address = $address;

        return $this;
    }
}
<?php

declare(strict_types=1);

namespace App\Entity\Embeddable;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as Orm;

#[Orm\Embeddable]
class Address implements \Stringable
{
    #[Orm\Column(type: Types::STRING, length: 255)]
    private ?string $street = null;

    #[Orm\Column(type: Types::STRING, length: 255, nullable: true)]
    private ?string $additionalInformation = null;

    #[Orm\Column(type: Types::STRING, length: 10)]
    private ?string $postalCode = null;

    #[Orm\Column(type: Types::STRING, length: 100)]
    private ?string $city = null;

    #[Orm\Column(type: Types::STRING, length: 2)]
    private ?string $country = null;

    public function getStreet(): ?string
    {
        return $this->street;
    }

    public function setStreet(?string $street): static
    {
        $this->street = $street;

        return $this;
    }

    public function getAdditionalInformation(): ?string
    {
        return $this->additionalInformation;
    }

    public function setAdditionalInformation(?string $additionalInformation): static
    {
        $this->additionalInformation = $additionalInformation;

        return $this;
    }

    public function getPostalCode(): ?string
    {
        return $this->postalCode;
    }

    public function setPostalCode(?string $postalCode): static
    {
        $this->postalCode = $postalCode;

        return $this;
    }

    public function getCity(): ?string
    {
        return $this->city;
    }

    public function setCity(?string $city): static
    {
        $this->city = $city;

        return $this;
    }

    public function getCountry(): ?string
    {
        return $this->country;
    }

    public function setCountry(?string $country): static
    {
        $this->country = $country;

        return $this;
    }
}

Possible Solution

No response

Additional Context

Uncaught PHP Exception ReflectionException: "Property App\Entity\Establishment::$location.address.postalCode does not exist" at UniqueEntityValidator.php line 298 {"exception":"[object] (ReflectionException(code: 0): Property App\\Entity\\Establishment::$location.address.postalCode does not exist at /Users/romain/Dev/guestspot/vendor/symfony/doctrine-bridge/Validator/Constraints/UniqueEntityValidator.php:298)"}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy