2

I'm using Symfony2 and i have created a many-to-many relationship using these Entities:

<?php
/** @Entity **/
class User
{
    // ...

    /**
     * @ManyToMany(targetEntity="Group", inversedBy="users")
     * @JoinTable(name="users_groups")
     **/
    private $groups;

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

    // ...
}

/** @Entity **/
class Group
{
    // ...
    /**
     * @ManyToMany(targetEntity="User", mappedBy="groups")
     **/
    private $users;

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

    // ...
}

If i update my DB i get this MySQL Schema:

CREATE TABLE User (
    id INT AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE users_groups (
    user_id INT NOT NULL,
    group_id INT NOT NULL,
    PRIMARY KEY(user_id, group_id)
) ENGINE = InnoDB;
CREATE TABLE Group (
    id INT AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE users_groups ADD FOREIGN KEY (user_id) REFERENCES User(id);
ALTER TABLE users_groups ADD FOREIGN KEY (group_id) REFERENCES Group(id);

I need to know how can i insert a couple into the relationship table called 'users_groups' to rappresent the association between a user and a group.

When i want to insert data into a database i usually try to persist these data using the Entity associated to the DB table but in this case i can't access this Entity because the table 'users_groups' was generated by the relationship, without an Entity class.

Thanks in advance for your help.

Ema.jar
  • 2,370
  • 1
  • 33
  • 43
  • I think that this link can help you a lot. http://stackoverflow.com/questions/7044864/symfony2-doctrine-manytomany-relation-is-not-saved-to-database – shmoolki Oct 26 '14 at 22:34
  • I know that link unfortunately i was looking for an example related to my code. ;) – Ema.jar Oct 26 '14 at 23:08

1 Answers1

3

Ok, so moving out from relational model to object model can be a bit overwhelming at the beginning.

As you noticed, you have no access to middle table but only to it's endpoints. Notice the User constructor:

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

It created a collection (basically the list) of objects of other side of the relation. That's the way to can manage the middle table - by inserting objects into the collection.

Try thinking about it. The N:M relation (which you're trying to achieve) is actually nothing more than a set of Users with each of them containing the list of Groups.

So, without further ado, this is what you should be able to do:

$em = ... // instance of entity manager

$user = new User();

$g1 = new Group();
$g2 = new Group();

$user->getGroups()->add($g1);
$user->getGroups()->add($g2);

$em->persist($g1);
$em->persist($g2);
$em->persist($user);

$em->flush();

There a lot of details on this topic but I think you should be able to kickstart with this right here. Hope it helps...

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85