简体   繁体   English

如何在Symfony2中使用Doctrine进行多对多查询

[英]How can I do a manyTomany query with Doctrine in Symfony2

I would like to know how many "afiliados" have one "Campana" 我想知道有多少个“会员”有一个“坎帕纳”

How does the query have to be? 查询必须如何?

My classes are: 我的课程是:

<?php


namespace Axonsystem\Bundles\CampanaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="afiliado")
 */
class Afiliado
{

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255,nullable=true)
 */
private $name;


/**
 * @ORM\ManyToMany(targetEntity="Campana", inversedBy="afiliados")
 * @ORM\JoinTable(name="afiliado_campana")
 *
 */
protected $campanas;


..........

/**
 * @ORM\Entity
 * @ORM\Table(name="campana")
 */
class Campana {


/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;


/**
 * @ORM\ManyToMany(targetEntity="Afiliado", mappedBy="campanas",cascade={"persist"})
 *
 */
protected $afiliados;


..............

I took away the setters and getters and some atributes, so that, the content shows easier. 我删除了设置者和获取者以及一些属性,因此,显示内容更加容易。

Using query builder, I think something like this would do: 使用查询生成器,我认为这样可以:

$qb->select("COUNT(f) as num_afiliados")
    ->from('CampanaBundle\Entity\Campana', 's')
    ->innerJoin('s.afiliados', 'f')
    ->where('s.id=:id')
    ->setParameter('id',$id);

$id contains the id of the campana you want to find out the number of afiliados. $ id包含您要查找辅助成员数量的Campana的ID。

UPDATE 更新

if you want to retrieve the name of the afiliados 如果要检索附属机构的名称

$qb->select("f.name")
        ->from('CampanaBundle\Entity\Campana', 's')
        ->innerJoin('s.afiliados', 'f')
        ->where('s.id=:id')
        ->setParameter('id',$id);

if you're working with Symfony just adapt the query. 如果您使用的是Symfony,则只需修改查询即可。 Supposing you're in a controller: 假设您在控制器中:

$ret = $this->getDoctrine()->getEntityManager()->createQueryBuilder()
                ->select("f.name")
                ->from('CampanaBundle:Campana', 's')
                ->innerJoin('s.afiliados', 'f')
                ->where('s.id=:id')
                ->setParameter('id', $id)
                ->getQuery()
                ->getResult();

You need to use a QueryBuilder to perform this query. 您需要使用QueryBuilder来执行此查询。 I recommend create a Repository for Entity Afiliado, and within the scribes something like this: 我建议为实体Afiliado创建一个存储库,并在抄写内创建如下内容:

<?php

namespace Axonsystem\Bundles\CampanaBundle\Entity\Repository;

use Doctrine\ORM\EntityRepository;

class AfiliadoRepository extends EntityRepository
{
    public function countByCampana($campanaId) {
        return $this->getEntityManager()->createQueryBuilder()
            ->select('COUNT(a.id)')
            ->from('CampanaBundle:Afiliado', 'a')
            ->innerJoin('CampanaBundle:Campana', 'c', 'WITH', 'c.id = a.campanas')
            ->where('c.id = :campanaId')
            ->setParameter('campanaId', $campanaId)
            ->getQuery()
            ->getSingleScalarResult();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM