1

我有这个具有以下字段的实体:

Andrei\StatisticsBundle\Entity\Visit:
    type: entity
    table: visits
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        counter:
            type: integer
        createdAt:
            type: datetime
            column: created_at
    manyToOne:
        visitor:
            targetEntity: Andrei\StatisticsBundle\Entity\Visitor
            inversedBy: visits
            joinColumn:
                name: visitor_id
                referencedColumnName: id
        website:
            targetEntity: Andrei\StatisticsBundle\Entity\Website
            inversedBy: visits
            joinColumn:
                name: website_id
                referencedColumnName: id
        country:
            targetEntity: Andrei\StatisticsBundle\Entity\Country
            inversedBy: visits
            joinColumn:
                name: country_id
                referencedColumnName: id

我想执行一个简单的选择条件并为 website_id 数据库列添加一个位置,如下所示,但它不起作用。我相信 website_id 现在被视为访问实体的字段,它是网站对象的属性,但我看不出如何进行这些类型的查询:

$em = $this->getDoctrine()->getManager();

$repository = $em->getRepository('AndreiStatisticsBundle:Visit');

$qb = $repository->createQueryBuilder('v')
    ->select('SUM(v.counter) AS views, v.createdAt AS day')
    ->where('v.website.id = :website_id')
    ->setParameter('website_id', 3)
    ->groupBy('v.createdAt');

$visits = $qb->getQuery()->getScalarResult();
4

2 回答 2

3

您必须使用 getReference 来获取网站类的对象而不添加查询,然后将其链接为参数

$em = $this->getDoctrine()->getManager();

$website    = $em->getReference('AndreiStatisticsBundle:Website', 3);

$repository = $em->getRepository('AndreiStatisticsBundle:Visit');

$qb = $repository->createQueryBuilder('v')
    ->select('SUM(v.counter) AS views, v.createdAt AS day')
    ->where('v.website = :website')
    ->setParameter('website', $website)
    ->groupBy('v.createdAt');

$visits = $qb->getQuery()->getScalarResult();
于 2013-11-14T13:12:38.363 回答
0

使用leftJoininnerJoin

$em = $this->getDoctrine()->getManager();

$repository = $em->getRepository('AndreiStatisticsBundle:Visit');

$qb = $repository->createQueryBuilder('v')
    ->select('SUM(v.counter) AS views, v.createdAt AS day')
    ->innerJoin('v.website', 'w')
    ->where('w.id = :website_id')
    ->setParameter('website_id', 3)
    ->groupBy('v.createdAt');

$visits = $qb->getQuery()->getScalarResult();
于 2013-11-14T13:14:49.647 回答