Query über eine Viele-zu-Viele-Beziehung mit Doctrine with Symfony2

Ich versuche zu verstehen, wie die vielen zu vielen Beziehungen mit Doctrine und Symfony2 funktionieren.

Ich habe das in der offiziellen Dokumentation gezeigte Beispiel neu erstellt (goo.gl/GYcVE0) und ich habe zwei Entitätsklassen:Benutze undGrupp wie Sie unten sehen können.

<?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();
    }

    // ...
}

Wenn ich meine Datenbank aktualisiere, erhalte ich das folgende 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);

Das Problem ist, dass ich in Symfony2 das @ braucEntitä, um eine Abfrage zu generieren. In diesem Fall ist der Tabelle keine Entität zugeordnet.users_group weil diese Tabelle automatisch vom Framework erstellt wird.

Also, wie kann ich die Informationen zu dieser Beziehungstabelle abrufen? Zum Beispiel muss ich alle Benutzer in einer Gruppe abrufen, die die Benutzer sind, die eine ID haben, die in der Tabelle @ angezeigt wirusers_group.

Wie kann ich das mit DQL, QueryBuilder oder anderen Methoden machen?

Danke vielmals

Antworten auf die Frage(1)

Ihre Antwort auf die Frage