DQL复杂查询,嵌套查询

时间:2016-02-09 15:19:20

标签: symfony doctrine-orm dql

我试图提取一些统计数据,我需要构建一个DQL查询,到目前为止我看起来像这样。

'SELECT COUNT(l) FROM AffiliateBundle:Lead l WHERE l.campaign = :campaign AND
l.customer.subscription IS NOT NULL AND
l.createdAt BETWEEN :date1 AND :date2'

现在,问题是我不能这样做:l.customer.subscription

我需要计算与特定广告系列相关联的订阅的所有潜在客户。

相关实体标记如下所示:

class Lead extends BaseEntity
{
    /**
     * @ORM\OneToOne(targetEntity="Customer", inversedBy="", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
     */
    private $customer;
}

class Customer extends BaseEntity
{
   /**
    * @ORM\OneToOne(targetEntity="Subscription", inversedBy="", cascade={"persist"})
    * @ORM\JoinColumn(name="subscription_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
    */
    private $subscription;
}

订阅是一个单独的实体,但这不相关。

1 个答案:

答案 0 :(得分:1)

你应该用JOIN

来做
'SELECT COUNT(l) FROM AffiliateBundle:Lead l 
JOIN AffiliateBundle:Customer c
WHERE l.campaign = :campaign AND
c.subscription IS NOT NULL AND
l.createdAt BETWEEN :date1 AND :date2'