PHP code example of arxy / doctrine-orm-filters

1. Go to this page and download the library: Download arxy/doctrine-orm-filters library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arxy / doctrine-orm-filters example snippets



declare(strict_types=1);

namespace App\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

class QuestionRepository extends EntityRepository
{
    use Arxy\DoctrineORMFilters\RepositoryFilters;

    public function getFilters(): array
    {
        return [
            'ids' => static function (FilterQueryBuilder $qb, array $ids): void {
                /* @var (int|string)[] $ids */
                $qb->queryBuilder->andWhere($qb->alias . '.id IN(:ids)')
                    ->setParameter('ids', $ids);
            },
        ];
    }
}

// Create Query builder by filter and returns it for further modification, or pagination
$fqb = $repository->createQueryBuilderByFilters('question', [
    'filterName' => 5
]);
//... do other stuff with $qb
$questions = $fqb->queryBuilder->getQuery()->getResult();

// or directly find by filters

$questions = $repository->findByFilters([
    'filterName' => 5
]);

// or counts by filter

$questionsCount = $repository->countByFilters([
    'filterName' => 5
]);

// or finds one result by filter (uses internal ->getOneOrNullResult(); of QueryBuilder)
try {
    $question = $repository->findOneByFilters([
        'filterName' => 5
    ]);
} catch (\Doctrine\ORM\NonUniqueResultException $exception) {
// Exactly one result was expected, 1+ found.
}

// or finds one result by filter (uses internal ->getSingleResult(); of QueryBuilder)
try {
    $question = $repository->getSingleResultByFilters([
        'filterName' => 5
    ]);
} catch (\Doctrine\ORM\NoResultException $exception) {
// Question wasn't found
}

// you can even create your own QueryBuilder and manually append filters to it.

$fqb = $repository->createQueryBuilder('question');
$fqb->appendFilter('ids', [5]);
//... do other stuff with $qb
$results = $qb->getQuery()->getResult();


declare(strict_types=1);

namespace App\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

class QuestionRepository extends EntityRepository
{
    use \Arxy\DoctrineORMFilters\RepositoryFilters;

    public function getFilters(): array
    {
        return [
            'joinX' => static function (FilterQueryBuilder $qb, boolean $value): void {
                $qb->queryBuilder->join(...)
            },
            'x' => static function (FilterQueryBuilder $qb, array $ids): void {
                $fqb->appendFilter('joinX', true);
                
                ....
            },
        ];
    }
}


declare(strict_types=1);

namespace App\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

class QuestionRepository extends EntityRepository
{
    use \Arxy\DoctrineORMFilters\RepositoryFilters;

    public function getFilters(): array
    {
        return [
             'id' => static function (FilterQueryBuilder $qb, array $id): void {
                  $qb->queryBuilder->andWhere($qb->alias . '.id IN(:ids)')
                    ->setParameter('ids', $ids);;
            },
        ];
    }
}


declare(strict_types=1);

namespace App\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

class AnswerRepository extends EntityRepository
{
    use \Arxy\DoctrineORMFilters\RepositoryFilters;

    public function getFilters(): array
    {
        return [
             'questionIds' => static function (FilterQueryBuilder $qb, array $ids): void {
                $qb->queryBuilder->join($qb->alias.'.question', 'question');
                $filterQb = new FilterQueryBuilder(queryBuilder: $qb->queryBuilder, alias: 'question', filters: $this->questionsRepository->getFilters());
                $filterQb->appendFilter('ids', $ids);
            },
        ];
    }
}