Symfony2 and doctrine: multiple relationships between two classes

I am new to Symfony2 and Doctrine. I have two classes: "User and Book". Each user may like very much or not at all like every book in the database.

To describe this association, I've defined three different ManyToMany relationships between the two classes. Here's an excerpt of my code:

Here is the User class

<?php

namespace MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="MyBundle\Entity\UserRepository")
 */
class User
{
   /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

   /**
    * @var string
    *
    * @ORM\Column(name="username", type="string", length=64, unique=true)
    */
    private $username;

   /**
    * @ORM\ManyToMany(targetEntity="Book", inversedBy="user_likemuch")
    * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
    */
    protected $books_likemuch;

   /**
    * @ORM\ManyToMany(targetEntity="Book", inversedBy="user_likequite")
    * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
    */
    protected $books_likequite;

   /**
    * @ORM\ManyToMany(targetEntity="Book", inversedBy="user_dislike")
    * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
    */
    protected $books_dislike;

    public function __construct()
    {
        $this->books_likemuch = new ArrayCollection();
        $this->books_likequite = new ArrayCollection();
        $this->books_dislike = new ArrayCollection();

    }

}

      


And here's a cool book:

<?php

namespace MyBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Book
 *
 * @ORM\Table(name="book")
 * @ORM\Entity(repositoryClass="MyBundle\Entity\BookRepository")
 */
class Book
{
   /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

   /**
    * @var string
    *
    * @ORM\Column(name="name", type="string", length=64)
    */
    private $name;

   /**
    * @ORM\ManyToMany(targetEntity="User", mappedBy="books_likemuch")
    */
    protected $user_likemuch;

   /**
    * @ORM\ManyToMany(targetEntity="User", mappedBy="books_likequite")
    */
    protected $user_likequite;

   /**
    * @ORM\ManyToMany(targetEntity="User", mappedBy="books_dislike")
    */
    protected $user_dislike;


    public function __construct() {
        $this->user_likemuch = new ArrayCollection();
        $this->user_likequite = new ArrayCollection();
        $this->user_dislike = new ArrayCollection();
    }

}

      


However, when I try to update the database schema with

php app/console doctrine:schema:update --force

      

I am getting the following error:

[Doctrine \ DBAL \ Schemas \ SchemaException]
A table named "XXX / myproject / app / data / users.user_book" already exists.



In fact, I don't even understand if I am describing the relationship incorrectly, or if the doctrine cannot manage multiple relationships between two classes.
Does anyone have any suggestions?
Thanks in advance!

+3


source to share


1 answer


Doctrine can indeed govern multiple relationships. The classic example is between users and roles. The problem here is that you are trying to create multiple ManyToMany relationships between the same two objects. What Doctrine does to manage this relationship is create a "join" table using table1_table2, or in your case "user_book". So the reason you are getting Doctrine error is ostensibly because it tries to create the same table 3 times.

I think you want to make 3 separate ManyToMany "join" tables, one for likemuch, likequite and dislike. So try listing a join table for each association and see if that helps. Example:



/**
 * @ORM\ManyToMany(targetEntity="Book", inversedBy="user_likemuch")
 * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
 * @ORM\JoinTable(name="user_book_likemuch")
 */
protected $books_likemuch;

      

See the section on Bidirectional Association Mapping for ManyToMany Relationships: Doctrine Association Mapping

+3


source







All Articles