2014-10-24 9 views
12

Sono triyng per capire come la relazione molti a molti funzioni con Doctrine e Symfony2.Query su una relazione molti-a-molti usando Doctrine con Symfony2

ho ricreato l'esempio illustrato nella documentazione ufficiale (goo.gl/GYcVE0) e ho due classi di entità: utente e Gruppo come potete vedere qui sotto.

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

    // ... 
} 

se aggiorno il mio DB ottengo questo schema MySQL:

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); 

Il problema è che in Symfony2 ho bisogno della Entity per generare una query e in questo caso non hanno un'entità associata alla tabella users_group perché questa tabella viene creata automaticamente dal framework.

Quindi, come posso recuperare le informazioni relative a questa tabella delle relazioni? Per esempio ho bisogno di ottenere tutti gli utenti in un gruppo che sono gli utenti che hanno un id che appare nella tabella users_group.

Come posso farlo utilizzando DQL, QueryBuilder o altri metodi?

Grazie mille.

+0

Qual è il tuo criterio puoi scrivere query mysql per i tuoi criteri? quindi proveremo a tradurlo nella query DQL –

+0

@M Khalid Junaid Ad esempio, ho bisogno di ottenere tutti gli utenti in un gruppo. L'utilizzo di SQL raw dovrebbe essere qualcosa come SELECT u.id, u.name Dagli Utenti u, Gruppo g, users_group ug WHERE ug.group_id = 5 AND u.id = ud.user_id. Come puoi vedere ho bisogno dell'entità relativa alla tabella users_group. –

risposta

21

È possibile scrivere una query join DQL come di seguito

$em = $this->getContainer()->get('doctrine')->getManager(); 
$repository = $em->getRepository('YourNamespaceYourBundle:User'); 
$query = $repository->createQueryBuilder('u') 
    ->innerJoin('u.groups', 'g') 
    ->where('g.id = :group_id') 
    ->setParameter('group_id', 5) 
    ->getQuery()->getResult(); 

vostro mappatura per groups proprietà a User entità gestirà unirsi a parte per sé non devi parlare della tabella di congiunzione nella query DQL

+1

Grazie, molto utile ;-) –

+0

Quale dovrebbe essere la query se il nome del gruppo e non l'id di gruppo? – Vidhi

+1

@Vidhi regola la parte where con nome di gruppo qualcosa come '-> where ('g.name =: group_name') -> setParameter ('nome_gruppo', $ nome_gruppo)' –

Problemi correlati