Symfony2 doctrine ManyToMany relation

会有一股神秘感。 提交于 2019-12-25 03:29:13

问题


I'm a beginner on Synfony2 and doctrine usage. I've created two entities as following :

For EndUser Entity (extension of FOSUserBundle ):

/**
 * EndUser
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Core\CustomerBundle\Entity\EndUserRepository")
 */
class EndUser extends BaseUser {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Core\GeneralBundle\Entity\Discipline", mappedBy="endusers")
     */
    private $discipline;

and for discipline entity

class Discipline {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="Core\CustomerBundle\Entity\EndUser", inversedBy="discipline")
     */
    private $endusers;

When I did "php app/console doctrine:schema:update -- force,

EndUser, Discipline and discipline_enduser tables have been created.

When I run a standard SQL request through phpmyadmin as :

select t0.*
from
discipline t0,
discipline_enduser t2
where 
t2.enduser_id = 1
and 
t2.discipline_id = t0.id

I obtain the expected result as the list of discipline for a specific user.

My concern is how to implement the same using the Entity with Symfony2 & Doctrine


回答1:


First I would recommend to use more natural language names for you relationships, like disciplines and endUsers since it is a manyToMany bond. Also, you need to create the get and set method for each. After you have prepared all your properties for an entity you should run the command to Generate your getters and setters

//this will generate all entities
php app/console doctrine:generate:entities BundleNamespace

After that, you can do things like :

$endUser->getDisciplines(); //return all disciplines of this user
$endUser->addDiscipline($someDiscipline); //add another discipline
$endUser->removeDiscipline($iAmABadDiscipline); //remove this discipline from this user
array $disciplines = [ ... ];
$endUser->setDisciplines($disciplines); // set multiple disciplines

Same logic applies to Discipline entity, ofcourse you should update them with an EntityManager for which you can read more in the answer here.




回答2:


All you really need to do is amend your EndUser entity to include the following:

use Doctrine\Common\Collections\ArrayCollection;

class EndUser extends BaseUser 
{
    private $discipline;

    public function __construct()
    {
        $this->discipline = new ArrayCollection();
    }

    public function setDiscipline($discipline)
    {
        $this->discipline = $discipline;

        return $this;
    }

    public function getDiscipline()
    {
        return $this->discipline;
    }
}

Then similarly amend Discipline, substituting endUser for discipline.

There is no need to create the intermediate entity. Doctrine has already figured it out. Neat, huh?!




回答3:


To make things more coherent, rename your class member to $disciplines (it's many-to-many, so there may be multiple disciplines in it). Then you need setters and getters in your entity to access the disciplines.

One way is to do this on the command line:

php app/console doctrine:generate:entities YourBundle:EndUser

This will add the required methods to your class. The unmodified version of the class file is backed up in EndUser.php~ in case anything is overwritten.

After doing this, you can just call the getter to obtain the disciplines:

$disciplines = $anEndUser->getDisciplines();

For example in a controller method:

public function someAction()
{
    $anEndUser = $this
        ->getDoctrine()
        ->getManager()
        ->getRepository('YourBundle:EndUser')
        ->find(1)
    ;
    $disciplines = $anEndUser->getDisciplines();
}


来源:https://stackoverflow.com/questions/25222968/symfony2-doctrine-manytomany-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!