Hubert Szkudlarek
Hubert Szkudlarek

Reputation: 33

Doctrine, Many to Many relationship issue

I have Entities: User and Catalog, related relations (as below). The user can create catalogs, and then can share them with other users. I want to search for all the directories assigned to the user (that is, those which he created himself and those that were made available to him).

public function findAllCatalogForUser($id){
    return $this->createQueryBuilder('catalog')
        ->leftJoin('catalog.users','us')
        ->innerJoin('catalog.user','u')
        ->where('u.id = :id OR us.id =:id ') 
        ->setParameters([':id'=>$id])
        ->getQuery()
        ->getResult();
}

However, when the query is executed, it receives an error:

Type of association must be one of _TO_ONE OR MANY_TO_MANY

It seems to me that the relationships between the tables are correct, but I can not make the query. How to fix this problem? Thank you in advance for your answer.

User Entity

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @UniqueEntity(fields="email", message="Ten email już istnieje, musisz użyć innego.")
 * @UniqueEntity(fields="username", message="Nazwa użytkownika już istnieje, musisz użyć innej.")
 */
class User implements AdvancedUserInterface, \Serializable
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=255, unique=true)
     * @Assert\NotBlank(message="Pole nie może być puste!")
     */
    private $username;


    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Catalog", inversedBy="user")
     * @ORM\JoinTable(name="user_catalog",
     *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="catalog_id", referencedColumnName="id")} )
     */
    protected $catalogs;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Catalog", mappedBy="user")
     */
    protected $catalog;

Catalog Entity

/**
 * Catalog
 *
 * @ORM\Table(name="catalog")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CatalogRepository")
 */
class Catalog
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="catalog")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;


    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\User", mappedBy="catalog")
     */
    protected $users;





    /**
     * Constructor
     */
    public function __construct()
    {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();

    }

Upvotes: 2

Views: 109

Answers (1)

Liora Haydont
Liora Haydont

Reputation: 1283

In your user entity:

/**
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Catalog", inversedBy="users")
 * @ORM\JoinTable(name="user_catalog",
 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="catalog_id", referencedColumnName="id")} )
 */
protected $catalogs;

In your catalog entity:

 /**
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\User", mappedBy="catalogs")
 */
protected $users;

Upvotes: 2

Related Questions