Skip to content

[PropertyInfo] [Validator] the new auto mapping is not working with non found property #31796

@elhadraoui

Description

@elhadraoui

Symfony version(s) affected: 4.3.0

Description
When we are using a function like public function addSomething() and no property with name: something not existing the exception "The entity has no field with name ..." is thrown.

How to reproduce
Create an entity with function addSomething() without property something and enable the auto mapping for validation.

Possible Solution

Additional context

Activity

cronk1986

cronk1986 commented on Jun 2, 2019

@cronk1986

Today I updated Symfony 4.2 to 4.3 and now I can't save entities that implement Interfaces or use traits, Mapping/PropertyMetadata class try to find a property in a interface and throw an error, my code snippet:

interface GetterIdInterface
{
  public function getId();
}
class MyEntity implements GetterIdInterface
{

    /**
     * @var string
     *
     * @ORM\Column(name="id", type="guid", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
     * @Assert\NotBlank()
     */
    protected $title;

    public function getId()
    {
        return $this->id;
    } 

    /**
     * Set title.
     *
     * @param string $title
     *
     * @return MyEntity
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title.
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    } 
 
}
//When I try to save MyEntity throw an exception:
...
$entity = new MyEntity();
$em->persist($entity);
$em->flush();
...
Property "title" does not exist in class "GetterIdInterface"

Symfony\Component\Validator\Exception\ValidatorException in vendor/symfony/validator/Mapping/PropertyMetadata.php (line 40)
 /* @throws ValidatorException     */    
public function __construct(string $class, string $name)    
{        
    if (!property_exists($class, $name)) {            
        throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s"', $name, $class));        
    }        
    parent::__construct($class, $name, $name);    
}

any idea?

linaori

linaori commented on Jun 3, 2019

@linaori
Contributor

Is this issue related to #31715?

cronk1986

cronk1986 commented on Jun 3, 2019

@cronk1986

I don't know if it's exactly the same, because I can save all my entities if they not implement an Interface o use Traits...
At the end maybe is the same issue.

Aerendir

Aerendir commented on Jun 5, 2019

@Aerendir
Contributor

I copy and paste here for clarity and more context, my (closed) issue (#31873):

Symfony version(s) affected: 4.3.0

Description
In one of my forms I receive this error:

Property "domain" does not exist in class "App\Entity\MyEntity"

Obviously it doesn't exist as the property is actually $domains.

So I have a method addDomain and a method removeDomain.

The problem is caused by the validation auto_mapping: if I disable it, the problem disappears

framework:
    validation:
        email_validation_mode: html5

        # Enables validator auto-mapping support.
        # For instance, basic validation constraints will be inferred from Doctrine's metadata.
        #auto_mapping:
        #    App\Entity\: []

The PropertyInfo causes a lot of troubles: it tries to guess properties starting from methods: this causes a lot of strange behaviors. Not strictly related to this, this is another example: php-translation/symfony-bundle#309

weaverryan

weaverryan commented on Jun 7, 2019

@weaverryan
Member

I can confirm this, here is a simple reproducer: https://github.com/weaverryan/symfony-automapper-reproducer - you can see the simple steps in the history:

  1. install validator & property-info
  2. Create a public setter method in src/Entity without a corresponding property
  3. Try to validate it (e.g. go to / in the reproducer):

Screen Shot 2019-06-07 at 11 13 06 AM

added a commit that references this issue on Jun 7, 2019
cronk1986

cronk1986 commented on Jun 8, 2019

@cronk1986

Perfect, now works!
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @weaverryan@nicolas-grekas@linaori@xabbuh@Aerendir

        Issue actions

          [PropertyInfo] [Validator] the new auto mapping is not working with non found property · Issue #31796 · symfony/symfony