Search code examples
symfonyormdoctrine-ormannotationsmany-to-many

Symfony Doctrine Many to Many with annotations


I have 3 classes with a relation ManyToMany (2 classes and other to produce this association). The classes are: User, Group and UserGroup (this one has extra fields).

The issues (2, but are similar)

AppBundle\Entity\User
The association AppBundle\Entity\User#usergroups refers to the owning side field AppBundle\Entity\UserGroup#user which is not defined as association, but as field. The association AppBundle\Entity\User#usergroups refers to the owning side field AppBundle\Entity\UserGroup#user which does not exist.

AppBundle\Entity\Group
The association AppBundle\Entity\Group#usergroups refers to the owning side field AppBundle\Entity\UserGroup#group which is not defined as association, but as field. The association AppBundle\Entity\Group#usergroups refers to the owning side field AppBundle\Entity\UserGroup#group which does not exist.

AppBundle/Entity/User.php

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\UserGroup;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**Id
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=15)
     */
    protected $phone;

    /**
     * @ORM\OneToMany(targetEntity="UserGroup", mappedBy="user")
     */
    private $usergroups;

    public function __construct()
    {
        parent::__construct();
        $this->usergroups = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set phone
     *
     * @param string $phone
     * @return User
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Get phone
     *
     * @return string 
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Add usergroups
     *
     * @param UserGroup $usergroup
     * @return User
     */
    public function addUsergroup(UserGroup $usergroup)
    {
        $this->usergroups[] = $usergroup;

        return $this;
    }

    /**
     * Remove usergroups
     *
     * @param UserGroup $usergroup
     */
    public function removeUsergroup(UserGroup $usergroup)
    {
        $this->usergroups->removeElement($usergroup);
    }

    /**
     * Get usergroups
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getUsergroups()
    {
        return $this->usergroups;
    }
}

AppBundle/Entity/Group.php

<?php

// src/AppBundle/Entity/Group.php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\Group as BaseGroup;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\UserGroup;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\GroupRepository")
 * @ORM\Table(name="fos_group")
 *
 */
class Group extends BaseGroup
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", columnDefinition="ENUM('department', 'workgroup')")
     */
    protected $grouptype;

    /**
     * @ORM\OneToMany(targetEntity="UserGroup", mappedBy="group")
     */
    private $usergroups;


    public function __construct()
    {
        parent::__construct('',array());
        $this->usergroups = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Is the given User the author of this Post?
     *
     * @param User $user
     *
     * @return bool
     */
    public function isAdmin(User $user)
    {
        //$users_tmp = $this->getUsergroups();

        //exit(\Doctrine\Common\Util\Debug::dump($users_tmp));
        /*

         foreach ($this->usergroups as $user) {
            if ($user->)
            $valor = $valor * 2;
        }
        return $user->id() === $this->getId();
        */
        return true;
    }

    /**
     * Set grouptype
     *
     * @param string $grouptype
     * @return Group
     */
    public function setGrouptype($grouptype)
    {
        $this->grouptype = $grouptype;

        return $this;
    }

    /**
     * Get grouptype
     *
     * @return string 
     */
    public function getGrouptype()
    {
        return $this->grouptype;
    }

    /**
     * Add usergroups
     *
     * @param \AppBundle\Entity\UserGroup $usergroups
     * @return Group
     */
    public function addUsergroup(\AppBundle\Entity\UserGroup $usergroups)
    {
        $this->usergroups[] = $usergroups;

        return $this;
    }

    /**
     * Remove usergroups
     *
     * @param \AppBundle\Entity\UserGroup $usergroups
     */
    public function removeUsergroup(\AppBundle\Entity\UserGroup $usergroups)
    {
        $this->usergroups->removeElement($usergroups);
    }

    /**
     * Get usergroups
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getUsergroups()
    {
        return $this->usergroups;
    }
}

AppBundle/Entity/UserGroup.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserGroupRepository")
 * @ORM\Table(name="fos_user_group")
 */
class UserGroup
{

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

    /**
     * @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="usergroups", cascade={"persist"})
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    /**
     * @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Group", inversedBy="usergroups", cascade={"persist"})
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
     */
    private $group;

    /**
     * @ORM\Column(type="string")
     */
    private $role;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set user
     *
     * @param integer $user
     * @return UserGroup
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return integer 
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set group
     *
     * @param integer $group
     * @return UserGroup
     */
    public function setGroup($group)
    {
        $this->group = $group;

        return $this;
    }

    /**
     * Get group
     *
     * @return integer 
     */
    public function getGroup()
    {
        return $this->group;
    }

    /**
     * Set role
     *
     * @param string $role
     * @return UserGroup
     */
    public function setRole($role)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Get role
     *
     * @return string 
     */
    public function getRole()
    {
        return $this->role;
    }
}

Please, any help is welcome. Thanks...


Solution

  • First error:

    AppBundle\Entity\UserGroup#user
    

    You added mapping @ORM\Column(type="integer") - remove it, mapper knows how to define relationships.

        /**
         * @ORM\Column(type="integer")
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="usergroups", cascade={"persist"})
         * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
         */
        private $user;
    

    to

        /**
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="usergroups", cascade={"persist"})
         * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
         */
        private $user;
    

    Second error - the same situation:

        /**
         * @ORM\Column(type="integer")
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Group", inversedBy="usergroups", cascade={"persist"})
         * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
         */
        private $group;
    

    Column is something different than association. I think, thats all :)