学说:用条件计算实体的项目

时间:2021-08-13 06:47:49

How can I count an entity's items with a condition in Doctrine? For example, I realize that I can use:

如何用Doctrine中的条件计算实体的项目?例如,我意识到我可以使用:

$usersCount = $dm->getRepository('User')->count();

But that will only count all users. I would like to count only those that have type employee. I could do something like:

但这只会计算所有用户。我想只计算那些有型员工的人。我可以这样做:

$users = $dm->getRepository('User')->findBy(array('type' => 'employee'));
$users = count($users);

That works but it's not optimal. Is there something like the following:?

这可行,但它不是最佳的。有类似以下内容:?

$usersCount = $dm->getRepository('User')->count()->where('type', 'employee');

2 个解决方案

#1


18  

Well, you could use the QueryBuilder to setup a COUNT query:

好吧,您可以使用QueryBuilder来设置COUNT查询:

Presuming that $dm is your entity manager.

假设$ dm是您的实体经理。

$qb = $dm->createQueryBuilder();

$qb->select($qb->expr()->count('u'))
   ->from('User', 'u')
   ->where('u.type = ?1')
   ->setParameter(1, 'employee');

$query = $qb->getQuery();

$usersCount = $query->getSingleScalarResult();

Or you could just write it in DQL:

或者你可以用DQL写它:

$query = $dm->createQuery("SELECT COUNT(u) FROM User u WHERE u.type = ?1");
$query->setParameter(1, 'employee');

$usersCount = $query->getSingleScalarResult();

The counts might need to be on the id field, rather than the object, can't recall. If so just change the COUNT(u) or ->count('u') to COUNT(u.id) or ->count('u.id') or whatever your primary key field is called.

计数可能需要在id字段上,而不是对象,无法回忆。如果是这样,只需将COUNT(u)或 - > count('u')更改为COUNT(u.id)或 - > count('u.id')或调用主键字段。

#2


5  

This question is 3 years old but there is a way to keep the simplicity of the findBy() for count with criteria.

这个问题已经有3年了,但是有一种方法可以使用标准来保持findBy()的简单性。

On your repository you can add this method :

在您的存储库中,您可以添加此方法:

    public function countBy(array $criteria)
    {
        $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
        return $persister->count($criteria);
    }

So your code will looks like this :

所以你的代码看起来像这样:

$criteria = ['type' => 'employee'];
$users = $repository->findBy($criteria, ['name' => 'ASC'], 0, 20);
$nbUsers = $repository->countBy($criteria);

#1


18  

Well, you could use the QueryBuilder to setup a COUNT query:

好吧,您可以使用QueryBuilder来设置COUNT查询:

Presuming that $dm is your entity manager.

假设$ dm是您的实体经理。

$qb = $dm->createQueryBuilder();

$qb->select($qb->expr()->count('u'))
   ->from('User', 'u')
   ->where('u.type = ?1')
   ->setParameter(1, 'employee');

$query = $qb->getQuery();

$usersCount = $query->getSingleScalarResult();

Or you could just write it in DQL:

或者你可以用DQL写它:

$query = $dm->createQuery("SELECT COUNT(u) FROM User u WHERE u.type = ?1");
$query->setParameter(1, 'employee');

$usersCount = $query->getSingleScalarResult();

The counts might need to be on the id field, rather than the object, can't recall. If so just change the COUNT(u) or ->count('u') to COUNT(u.id) or ->count('u.id') or whatever your primary key field is called.

计数可能需要在id字段上,而不是对象,无法回忆。如果是这样,只需将COUNT(u)或 - > count('u')更改为COUNT(u.id)或 - > count('u.id')或调用主键字段。

#2


5  

This question is 3 years old but there is a way to keep the simplicity of the findBy() for count with criteria.

这个问题已经有3年了,但是有一种方法可以使用标准来保持findBy()的简单性。

On your repository you can add this method :

在您的存储库中,您可以添加此方法:

    public function countBy(array $criteria)
    {
        $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
        return $persister->count($criteria);
    }

So your code will looks like this :

所以你的代码看起来像这样:

$criteria = ['type' => 'employee'];
$users = $repository->findBy($criteria, ['name' => 'ASC'], 0, 20);
$nbUsers = $repository->countBy($criteria);