我的实体包含以下字段:
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
我想执行一个简单的选择条件,并为web_id数据库列添加一个像bellow一样的位置,但它不起作用。我相信website_id现在被视为Visit enity的一个字段,它相当于Website对象的属性,但我看不出如何进行这些类型的查询:
$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();
答案 0 :(得分: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();
答案 1 :(得分:0)
使用 leftJoin的 innerJoin
$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();