8

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.

tomloprod
  • 7,472
  • 6
  • 48
  • 66
konrad_firm
  • 859
  • 1
  • 11
  • 28
  • 1
    Ensure that in your `AppBundle\Entity\Role` class has a public property called by `$user` or a public getter method `getUser()`. – felipsmartins Feb 01 '16 at 14:35
  • 1
    Mybe you can check what does the `php bin/console doctrine:schema:valid `say? – Dominykas55 Feb 01 '16 at 14:36
  • @felipsmartins - but I don't have according to this doc: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-unidirectional - this is a unidirectional many to many relation. And besides, it used to work o_O – konrad_firm Feb 01 '16 at 14:38
  • @Dominykas55 it says "Class 'AppBundle\Entity\Sessions' does not exist", there indeed is a table named "sessions" in database, but I don't want to create an entity for it... – konrad_firm Feb 01 '16 at 14:40
  • 1
    When you ran the mapping command you generated mapping files under AppBundle/Resources/config/doctrine which are interfering with your annotations. Delete the directory. – Cerad Feb 01 '16 at 14:41
  • @Cerad Wow, it worked! Thank you. If you add this as answer I will mark it as accepted. Thank you all. – konrad_firm Feb 01 '16 at 14:44

2 Answers2

8

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.

Cerad
  • 48,157
  • 8
  • 90
  • 92
2

@Cerad, answer is correct. Just clear the cache (php bin/console cache:clear) and you should be good to go!

symfony.com/doc/current/console/usage.html

tfont
  • 10,891
  • 7
  • 56
  • 52
  • 3
    While I appreciate the vote of confidence, clearing the cache will not remove the extra yml/xml mapping files that were causing the problem. – Cerad Nov 22 '16 at 16:54
  • I'm using symfony 4 and removing cache was enough for me. – vimuth Jul 10 '18 at 07:59