在学说中使用 COUNT
Using COUNT in doctrine
我的存储库中有以下 dql class:
public function find2($id)
{
return $this->createQueryBuilder('b')
->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')
->leftJoin('b.branches', 'branch')
->leftJoin('branch.productAllocationItems','pai')
->leftJoin('pai.DRItems', 'dri')
->where('b.id = :id')
->setParameter('id', $id)
->andWhere('ct > 1')
->getQuery()
->getResult();
}
我的问题是 COUNT 似乎不起作用,我的查询有什么错误吗?
我只想 return 实体,其 DRItems 基于条件。谢谢
我收到以下错误:
Column not found: 1054 Unknown column 'sclr0' in 'where clause'
您可以通过更改 select
语句来获取计数。
根据 documentation select 可以使用以下格式
但是对于 count 你可以用逗号分隔的字段来实现 count ,就像我们在 SQL.
中做的一样
// Example - $qb->select('u')
// Example - $qb->select(array('u', 'p'))
// Example - $qb->select($qb->expr()->select('u', 'p'))
public function select($select = null);
改变
->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')
至
->select( 'b,branch, pai, dri ,COUNT(dri) AS ct')
使用
->having('ct > 1')
而不是
->andWhere('ct > 1')
public function find2($id)
{
return $this->createQueryBuilder('b')
->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')
->leftJoin('b.branches', 'branch')
->leftJoin('branch.productAllocationItems','pai')
->leftJoin('pai.DRItems', 'dri')
->where('b.id = :id')
->setParameter('id', $id)
->having('ct > 1')
->getQuery()
->getResult();
}
我的存储库中有以下 dql class:
public function find2($id)
{
return $this->createQueryBuilder('b')
->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')
->leftJoin('b.branches', 'branch')
->leftJoin('branch.productAllocationItems','pai')
->leftJoin('pai.DRItems', 'dri')
->where('b.id = :id')
->setParameter('id', $id)
->andWhere('ct > 1')
->getQuery()
->getResult();
}
我的问题是 COUNT 似乎不起作用,我的查询有什么错误吗? 我只想 return 实体,其 DRItems 基于条件。谢谢
我收到以下错误:
Column not found: 1054 Unknown column 'sclr0' in 'where clause'
您可以通过更改 select
语句来获取计数。
根据 documentation select 可以使用以下格式
但是对于 count 你可以用逗号分隔的字段来实现 count ,就像我们在 SQL.
中做的一样// Example - $qb->select('u')
// Example - $qb->select(array('u', 'p'))
// Example - $qb->select($qb->expr()->select('u', 'p'))
public function select($select = null);
改变
->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')
至
->select( 'b,branch, pai, dri ,COUNT(dri) AS ct')
使用
->having('ct > 1')
而不是
->andWhere('ct > 1')
public function find2($id)
{
return $this->createQueryBuilder('b')
->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')
->leftJoin('b.branches', 'branch')
->leftJoin('branch.productAllocationItems','pai')
->leftJoin('pai.DRItems', 'dri')
->where('b.id = :id')
->setParameter('id', $id)
->having('ct > 1')
->getQuery()
->getResult();
}