In my Symfony 3 project, I have a ManyToMany relation between "users" and "roles".
It used to work, but now I have an error:
Property AppBundle\Entity\Role::$user does not exist
I don't know what I did, probably it's because of running a "php bin/console doctrine:mapping:import --force AppBundle xml" command.
Here is a fragment of the User entity class:
/**
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Role", cascade = {"persist"})
* @ORM\JoinTable(name="user_role")
*/
private $roles;
As you can see there is a relation to the Role entity.
Role entity on the other hand doesn't contain any relation information, and it should work according to this article:
https://knpuniversity.com/screencast/symfony2-ep3/many-to-many-relationship
and it used to work, and now it doesn't and have no idea why.
As far as I understand it, this is named 'unidirectional ManyToMany" relation according to Symfony docs. And for me everything looks fine.
The PropertyAccess component provides functions to read and write from/to an object or array using a simple string notation. If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.
Although the Symfony Framework doesn't integrate any component to work with databases, it provides tight integration with a third-party library called Doctrine . Doctrine's sole goal is to give you powerful tools to make database interactions easy and flexible.
Warning: You are browsing the documentation for Symfony 3.4 , which is no longer maintained. Read the updated version of this page for Symfony 6.1 (the current stable version).
One mistake even seasoned developers make when starting a Symfony project is forgetting to set up default charset and collation on their database, ending up with latin type collations, which are default for most databases.
When you ran the mapping command you generated mapping files under AppBundle/Resources/config/doctrine which are interfering with your annotations. In Doctrine you can only have one type of entity mapping per bundle. Multiple types tend to fail silently and confusingly.
This explains why it "used to work".
Delete the config/doctrine directory, maybe clear the cache and you should be back to where you were before.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With