I use Symfony 2 with Doctrine, and I have two entities combined in many ways. Let's say I have two objects: "User and group", and the related tables on db are users, groups and users_groups.
I would like to get the 10 most popular groups in DQL, but I donβt know the syntax for executing queries in the connection table (users_groups). I already looked at the Doctrine manual, but I did not find a solution, I think I still have a lot to learn about DQL.
In simple sql, which will be:
select distinct group_id, count(*) as cnt from users_groups group by group_id order by cnt desc limit 10
Could you help me translate this to DQL?
Update (classes):
/** * Entity\E_User * * @ORM\Table(name="users") * @ORM\Entity */ class E_User { /** * @ORM\ManyToMany(targetEntity="E_Group", cascade={"persist"}) * @ORM\JoinTable(name="users_groups", * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="cascade")}, * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id", onDelete="cascade")} * ) */ protected $groups; /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $name * * @ORM\Column(name="name", type="string", length=255) */ private $name; /* ... other attributes & getters and setters ...*/ } /** * Entity\E_Group * * @ORM\Table(name="groups") * @ORM\Entity */ class E_Group { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $name * * @ORM\Column(name="text", type="string", length=255) */ private $name; /* ... other attributes & getters and setters ...*/ }
Lorenzo marcon
source share