DDC-2318: Count all element #2911

Closed
opened 2026-01-22 14:07:10 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 25, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user xzan:

Hello,

I'm sorry if it is not the right place to ask and suggest but I was not sure where to contact you.

I have been trying to make a pagination of articles without using DQL. For me, it defies the purpose of the ORM to do DQL for things like pagination.

After some research I found the method findBy() which allows me to select X element starting at position Y.
Yet, to know the number of pages, I needed to count the number of elements in my database and I didn't find any count() method in the Entity Repository.

I was wondering why there is no count method ?

Moreover, I did my own entity Repository that extends the default one to have a count method like this :


class CountableEntityRepository extends EntityRepository {

/****

  • returns the number of entity's rows
  • @return int
    */
    public function count() {
    $query = $this->createQueryBuilder('e')->select('count(e)')->getQuery();
    return $query->getSingleScalarResult();
    }

}```

as I am new to Doctrine I don't know if this is a good solution, if it is not, could you add a count method to the repository ?

Thank you very much.

Regards,
Benjamin.
Originally created by @doctrinebot on GitHub (Feb 25, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user xzan: Hello, I'm sorry if it is not the right place to ask and suggest but I was not sure where to contact you. I have been trying to make a pagination of articles without using DQL. For me, it defies the purpose of the ORM to do DQL for things like pagination. After some research I found the method findBy() which allows me to select X element starting at position Y. Yet, to know the number of pages, I needed to count the number of elements in my database and I didn't find any count() method in the Entity Repository. I was wondering why there is no count method ? Moreover, I did my own entity Repository that extends the default one to have a count method like this : ```use Doctrine\ORM\EntityRepository; class CountableEntityRepository extends EntityRepository { ``` /**** * returns the number of entity's rows * @return int */ public function count() { $query = $this->createQueryBuilder('e')->select('count(e)')->getQuery(); return $query->getSingleScalarResult(); } ``` }``` as I am new to Doctrine I don't know if this is a good solution, if it is not, could you add a count method to the repository ? Thank you very much. Regards, Benjamin.
admin added the New Feature label 2026-01-22 14:07:10 +01:00
admin closed this issue 2026-01-22 14:07:11 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 25, 2013):

Comment created by @ocramius:

Heya!

First of all, consider that this is an issue tracker, not a Q&A platform (use StackOverflow, the mailing list or IRC for that).

Anyway, the use case seems reasonable to me, but consider that Doctrine comes with a powerful paginator ( http://docs.doctrine-project.org/en/latest/tutorials/pagination.html )

I don't think this logic fits repositories by default.

@doctrinebot commented on GitHub (Feb 25, 2013): Comment created by @ocramius: Heya! First of all, consider that this is an issue tracker, not a Q&A platform (use StackOverflow, the mailing list or IRC for that). Anyway, the use case seems reasonable to me, but consider that Doctrine comes with a powerful paginator ( http://docs.doctrine-project.org/en/latest/tutorials/pagination.html ) I don't think this logic fits repositories by default.
Author
Owner

@doctrinebot commented on GitHub (Feb 25, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Feb 25, 2013): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2911