Symfony 如何创建用于选择多对多元素的查询
我的实体在多对多连接上:Symfony 如何创建用于选择多对多元素的查询,symfony,doctrine-orm,many-to-many,Symfony,Doctrine Orm,Many To Many,我的实体在多对多连接上: 实体-主对象 客户机-通过“实体\客户机”表与实体进行多对多访问 在.yml中配置 manyToMany: clients: targetEntity: Client joinTable: name: entity_clients joinColumns: taskpack_id: referencedColumnName: id inverseJoinColumns:
- 实体-主对象
- 客户机-通过“实体\客户机”表与实体进行多对多访问
manyToMany:
clients:
targetEntity: Client
joinTable:
name: entity_clients
joinColumns:
taskpack_id:
referencedColumnName: id
inverseJoinColumns:
client_id:
referencedColumnName: id
我有实体元素,我想获得附加到它的查询客户端。
我可以选择的客户:
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('TestGroupBundle:Entity')->find($id);
$clients = $entity->getClients();
但是我需要查询如何选择这些元素。我尝试编写查询,但不是这样:
$qb = $this->em->createQueryBuilder()
->select('c')
->from('TestGroupBundle:Entity', 't')
->join('t.clients', 'c')
->andWhere('t.id = :id')
->setParameter('id', $id);
但我得到了一个错误:
[Semantical Error] line 0, col -1 near 'SELECT c FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.
有人能帮忙吗?我想您需要将初始实体添加到select
->select('t, c')