use FrankDeJonge\DoctrineQuerySpecification\SpecificationAwareEntityRepository;
use FrankDeJonge\DoctrineQuerySpecification\SpecificationAwareRepository;
use FrankDeJonge\DoctrineQuerySpecification\SpecificationAwareRepositoryTrait;
class ArticleRepository extends SpecificationAwareEntityRepository
{
}
// OR
class ArticleRepository implements SpecificationAwareRepository
{
use SpecificationAwareRepositoryTrait;
}
use Doctrine\ORM\QueryBuilder;
use FrankDeJonge\DoctrineQuerySpecification\QueryConstraint;
class IsPublished implements QueryConstraint
{
public function asQueryConstraint(QueryBuilder $builder, string $rootAlias): ?object
{
$expr = $builder->expr();
return $expr->eq("{$rootAlias}.published", true);
}
}
$publishedArticles = $articleRepository->findBySpecification(new IsPublished);
use Doctrine\ORM\QueryBuilder;
use FrankDeJonge\DoctrineQuerySpecification\QueryConstraint;
class ArticleHasNameLike implements QueryConstraint
{
/** @var string */
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function asQueryConstraint(QueryBuilder $builder, string $rootAlias): ?object
{
$expr = $builder->expr();
$builder->setParameter('name_search', $this->name);
return $expr->like("{$rootAlias}.name", ':name_search');
}
}
$publishedArticles = $articleRepository->findBySpecification(new ArticleHasNameLike('Awesome Name'));
use Doctrine\ORM\Query;
use FrankDeJonge\DoctrineQuerySpecification\QueryModifier;
class AsArray implements QueryModifier
{
public function modifyQuery(Query $query, string $rootAlias): void
{
$query->setHydrationMode(Query::HYDRATE_ARRAY);
}
}
$publishedArticles = $articleRepository->findBySpecification(new AsArray);
use Doctrine\ORM\QueryBuilder;
use FrankDeJonge\DoctrineQuerySpecification\QueryBuilderModifier;
class InReverseOrder implements QueryBuilderModifier
{
public function modifyQueryBuilder(QueryBuilder $builder, string $rootAlias): void
{
$builder->orderBy("{$rootAlias}.id", "DESC");
}
}
$publishedArticles = $articleRepository->findBySpecification(new InReverseOrder);
$andSpecification = SpecificationCollection::all(
new IsPublished(),
new InReversedOrder(),
new WithAuthor(),
);
$orSpecification = SpecificationCollection::any(
new IsFeatured(),
new IsPublishedToday(),
);
use Doctrine\ORM\QueryBuilder;
use FrankDeJonge\DoctrineQuerySpecification\QueryConstraint;
class FeaturedFromAuthor implements QueryConstraint
{
public function __construct(Author $author)
{
$this->author = $author;
}
public function asQueryConstraint(QueryBuilder $queryBuilder, string $rootAlias): ?object
{
$expr = $queryBuilder->expr();
return $expr->andX(
(new FromAuthor($this->author))->asQueryConstraint($queryBuilder, $rootAlias),
(new FeaturedArticle)->asQueryConstraint($queryBuilder, $rootAlias),
);
}
}
use FrankDeJonge\DoctrineQuerySpecification\SpecificationCollection\All;
class FeaturedFromAuthor extends All
{
public function __construct(Author $author)
{
parent::__construct(
new FromAuthor($author),
new FeaturedArticle(),
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.