0

I need a ManyToMany relation between two classes: User and Document. I have the following:

Class User

   /**
     *  @ORM\Id
     *  @ORM\Column(type="string")
    */
    protected $guid; 


    /**
     * @ORM\ManyToMany(targetEntity="project\DocumentationBundle\Entity\Document", mappedBy="users", cascade={"persist", "remove"})
     **/
    protected $documents;

Class Document

   /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
    */
    protected $id_doc; 

   /**
     * @ORM\ManyToMany(targetEntity="project\UserBundle\Entity\User", inversedBy="documents")
     * @ORM\JoinTable(name="document_user")
     */
    protected $users;   

What the application should do is when the user publishes a document, he can select which users can see it. That's why I need that relation.

When I try to

php app/console doctrine:schema:update --dump-sql

it fires:

Column name 'id' referenced for relation from project\DocumentationBundle\Entity\Document towards project\UserBundle\Entity\User does not exist.

What am i doing wrong? Please help!!

bamalu
  • 25
  • 2
  • 8

1 Answers1

0

Please refer to this: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-unidirectional

Your example is using not standard column name for id field in your entity, its name is "guid" with string type instead of "id" name with integer type, but if you must use it - you can - but you have to use @joinColumns properly in your Document entity:

/**
 * @ManyToMany(targetEntity="project\UserBundle\Entity\User")
 * @JoinTable(name="users_documents",
 *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="guid")},
 *      inverseJoinColumns={@JoinColumn(name="document_id", referencedColumnName="id_doc")}
 *      )
 **/
Karol F
  • 1,556
  • 2
  • 18
  • 33