Saturday, February 20, 2021

Doctrine: Given object is not an instance of the class this property was declared in

 This error confused me a little bit. I was trying to add an EntityType field to my form and I was keep getting this error. 

Executing:

php bin/console doctrine:schema:validate

Was showing that the Doctrine annotations were correctly set, but still the error was there. Trying to manually query all records from that entity in controller showed me the problem:

$companies = $em->getRepository(Company::class)->findAll();

Looking at the list of companies it was a list of objects from User class! Evrika!  I've made the repository class for Company class by copy pasting it from User class and I forgot to change the entityClass parameter in constructor:

parent::__construct($registry, Company::class);

Have a bug free day!