Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Doctrine2如何创建聚合子查询_Php_Symfony_Doctrine Orm_Dql - Fatal编程技术网

Php Doctrine2如何创建聚合子查询

Php Doctrine2如何创建聚合子查询,php,symfony,doctrine-orm,dql,Php,Symfony,Doctrine Orm,Dql,我有下一张桌子 id int not null auto icrement email_name varchar event_type varchar 电子邮件类型-可能值(已发送、打开、单击、取消订阅) 我需要在doctrine2中创建这样的查询 SELECT COUNT(ee.id) as total, (SELECT COUNT(e.id) from email_statistic as e where e.event_type = 'open') as opened FROM emai

我有下一张桌子

id int not null auto icrement
email_name varchar
event_type varchar
电子邮件类型-可能值(已发送、打开、单击、取消订阅) 我需要在doctrine2中创建这样的查询

SELECT COUNT(ee.id) as total, (SELECT COUNT(e.id) from email_statistic as e where e.event_type = 'open') as opened FROM email_statistic as ee
我想选择总金额,以及有多少打开的电子邮件进行统计 我怎样才能做到wia原则

   $qb = $this->createQueryBuilder('ee')
            ->select('count(ee) As total');

$qb2 = $this->createQueryBuilder('e')
            ->select('count(e) As opened');

        $qb2
            ->andWhere('es.eventType = :eventType')
            ->setParameter('eventType', 'open');

$qb->addSelect($qb2) --- this does not allowed by doctrine

我应该使用本机查询吗?我可以用dql执行此操作吗?

首先,您应该创建一个实体,例如EmailStatistic:

/**
 * @ORM\Entity()
 * @ORM\Table()
 */
class EmailStatistic
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="email_type", type="string", length=255)
     */
    protected $emailType;

    /**
     * @var string
     *
     * @ORM\Column(name="email_name", type="string", length=255)
     */
    protected $emailName;
}
然后可以使用GeneralCaseExpression()


将第二个选择直接放入
$qb->select()
@mmmm不工作query builder通过DQL与实体一起工作。我在这里没有看到任何与实体相关的东西。您的猜测是正确的,您可能希望在这里使用本机查询。
$em = $this->get('doctrine')->getEntityManager();
$qb = $em->createQueryBuilder();
$result = $qb
    ->select('SUM(CASE WHEN (e.emailType = \'open\') THEN 1 ELSE 0 END) as opened')
    ->addSelect('SUM(e.id) as total')
    ->from('AppBundle:EmailStatistic', 'e')
    ->getQuery()
    ->getArrayResult();
// $result[0]['opened'] contains number of opened emails
// $result[0]['total'] contains total number of emails