Easyadminbundle: Could not determine access type for property - Symfony 4 bug ?

Created on 4 Jan 2018  路  5Comments  路  Source: EasyCorp/EasyAdminBundle

I'm trying to do a ManyToMany relation between two classes but i have constantly this error when i want to add :

Could not determine access type for property "users" in class "App\Entity\Subject".

Really don't understand why it dont work on easy admin ...

Here User class :

  /* * @ORM\ManyToMany(targetEntity="Subject", inversedBy="users", cascade={"persist"})
     * @ORM\JoinTable(name="subject_user")
     */
    private $subjects;



    public function __construct()
    {
        $this->createdAt = new \DateTime(date('Y-m-d H:i:s'));
        $this->sortRole = "ROLE_USER";
        $this->subjects = new ArrayCollection();
    }
    /**
     * Get the value of subjects
     */ 
    public function getSubjects()
    {
        return $this->subjects;
    }



    public function setSubject(\App\Entity\Subject $subject)
    {
        $subject->setUser($this);
        $this->subjects[] = $subject;
        return $this;
    }

    public function removeSubjects(Subject $subject){
        $this->subjects->removeElement($subject);
    }
}

Here Subject class :

    /**
     * @ORM\OneToMany(targetEntity="Mark", mappedBy="subject", cascade={"remove","persist"})
     */
    protected $mark;

    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="subjects")
     */
    private $users;


    public function __toString(){
        return $this->name;
    }

    public function __construct(){
        $this->users = new ArrayCollection();
    }

     /**
      * Get the value of users
      */ 
     public function getUsers()
     {
        return $this->users;
     }

    /**
     */
     public function setUser(\App\Entity\User $user)
     {
          //$user->setSubject($this);
          $this->users[] = $user;

          return $this;
     }

     public function removeUsers(User $user){
         $this->users->removeElement($user);
     }
}

and config easy_admin.yml:

        Inscrit:
            class: App\Entity\Subject
            form:
                fields:
                    - { property: 'name', label: 'Nom de la mati猫re' }
                    - { property: 'users', type_options: {by_reference: false} }

have idea ?

Most helpful comment

Reading this -> https://stackoverflow.com/questions/41213014/could-not-determine-access-type-for-property-file I think you must add addXXX() and removeXXX() methods in your entities and not only removeXXX(). In addition, and this is very important, the XXX must be the singular name of the entity, not the plural name: removeSubject() and addSubject(), etc.

All 5 comments

Reading this -> https://stackoverflow.com/questions/41213014/could-not-determine-access-type-for-property-file I think you must add addXXX() and removeXXX() methods in your entities and not only removeXXX(). In addition, and this is very important, the XXX must be the singular name of the entity, not the plural name: removeSubject() and addSubject(), etc.

Closing as probably fixed. Thanks!

Thanks @javiereguiluz !

I had the same problem with a OneToMany relation. I had just the getXXX() and addXXX() functions and not removeXXX() because I didn't need it, and that was what was wrong.

I added the removeXXX() function in my owner entity and it worked !

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();
...



md5-3c95ffaa0d586ef4dec2ad424f81f17a



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?

Let me throw a wrench in the plural vs singular logic here.
I have a contact entity with a "EmailAddress" property in it. The error states that it is looking for addEmailAddres which is illogical because both the singular and plural versions end in S. I'm going to change it to "email" as a work-around but this is a bit too nit-picky for my liking. In my case it's a many to many relationship with other entities pointing to this emailAddress table as well. It's kinda a pain if you ask me.

Was this page helpful?
0 / 5 - 0 ratings