Define custom function in custom repository #5232

Closed
opened 2026-01-22 15:02:08 +01:00 by admin · 1 comment
Owner

Originally created by @davidedonattini on GitHub (Aug 25, 2016).

Originally assigned to: @Ocramius on GitHub.

Hello,
I would like a private function in my custom repository for pagination the results but I can not because I get this error:
"Undefined method 'pagingResults'. The method name must start with either findBy or findOneBy!"

I check the Doctrine\ORM\EntityRepository and I found that the __call function force only functions which are prefixed with 'findBy' and 'findOneBy'.

Any suggestions?

This is my code in Symfony3 project:


// AppBundle\Repository\EntityRepository

namespace AppBundle\Repository;

use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;

class EntityRepository extends \Doctrine\ORM\EntityRepository
{
    private function pagingResults($queryBuilder, $limit, $page){

        $pagerAdapter = new DoctrineORMAdapter($queryBuilder);
        $query = $pagerAdapter->getQuery();
        $query->useResultCache(true, 3600, "some_id");
        $pager = new Pagerfanta($pagerAdapter);
        $pager->setCurrentPage($page);
        $pager->setMaxPerPage($limit);

        return $pager;
    }


}

// AppBundle\Repository\AgentRepository

namespace AppBundle\Repository;

use AppBundle\Repository\EntityRepository;

class AgentRepository extends EntityRepository
{
    public function findAllPaginated($limit = 10, $page = null, array $sorting = array())
    {
        $fields = array_keys($this->getClassMetadata()->fieldMappings);
        $queryBuilder = $this->createQueryBuilder('agent');

        foreach ($fields as $field) {
            if (isset($sorting[$field])) {
                $direction = ($sorting[$field] === "asc") ? "asc" : "desc";
                $queryBuilder->addOrderBy("a.".$field, $direction);
            }
        }

        if(isset($page)) {
            $results = $this->pagingResults($queryBuilder, $limit, $page);
        }

        return $results;
    }
}

Originally created by @davidedonattini on GitHub (Aug 25, 2016). Originally assigned to: @Ocramius on GitHub. Hello, I would like a private function in my custom repository for pagination the results but I can not because I get this error: "Undefined method 'pagingResults'. The method name must start with either findBy or findOneBy!" I check the Doctrine\ORM\EntityRepository and I found that the __call function force only functions which are prefixed with 'findBy' and 'findOneBy'. Any suggestions? This is my code in Symfony3 project: ``` // AppBundle\Repository\EntityRepository namespace AppBundle\Repository; use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Pagerfanta; class EntityRepository extends \Doctrine\ORM\EntityRepository { private function pagingResults($queryBuilder, $limit, $page){ $pagerAdapter = new DoctrineORMAdapter($queryBuilder); $query = $pagerAdapter->getQuery(); $query->useResultCache(true, 3600, "some_id"); $pager = new Pagerfanta($pagerAdapter); $pager->setCurrentPage($page); $pager->setMaxPerPage($limit); return $pager; } } ``` ``` // AppBundle\Repository\AgentRepository namespace AppBundle\Repository; use AppBundle\Repository\EntityRepository; class AgentRepository extends EntityRepository { public function findAllPaginated($limit = 10, $page = null, array $sorting = array()) { $fields = array_keys($this->getClassMetadata()->fieldMappings); $queryBuilder = $this->createQueryBuilder('agent'); foreach ($fields as $field) { if (isset($sorting[$field])) { $direction = ($sorting[$field] === "asc") ? "asc" : "desc"; $queryBuilder->addOrderBy("a.".$field, $direction); } } if(isset($page)) { $results = $this->pagingResults($queryBuilder, $limit, $page); } return $results; } } ```
admin added the Invalid label 2026-01-22 15:02:08 +01:00
admin closed this issue 2026-01-22 15:02:08 +01:00
Author
Owner

@Ocramius commented on GitHub (Aug 25, 2016):

Must be a public function, if you want it to be available from collaborators. protected if you use inheritance.

@Ocramius commented on GitHub (Aug 25, 2016): Must be a `public` function, if you want it to be available from collaborators. `protected` if you use inheritance.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5232