PHP code example of rikbruil / doctrine-specification
1. Go to this page and download the library: Download rikbruil/doctrine-specification 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/ */
rikbruil / doctrine-specification example snippets
// Not using the lib
// Note: Advertisement repository is an instance of the Doctrine default repository class
$qb = $this->em->getRepository('Advertisement')
->createQueryBuilder('r');
return $qb->where('r.ended = 0')
->andWhere(
$qb->expr()->orX(
'r.endDate < :now',
$qb->expr()->andX(
'r.endDate IS NULL',
'r.startDate < :timeLimit'
)
)
)
->setParameter('now', new \DateTime())
->setParameter('timeLimit', new \DateTime('-4weeks'))
->getQuery()
->getResult();
use Rb\Specification\Doctrine\Condition\Equals;
use Rb\Specification\Doctrine\Condition\IsNull;
use Rb\Specification\Doctrine\Condition\LessThan;
use Rb\Specification\Doctrine\Logic\AndX;
use Rb\Specification\Doctrine\Logic\OrX;
use Rb\Specification\Doctrine\Specification;
// Using the lib
$spec = new Specification([
new Equals('ended', 0),
new OrX(
new LessThan('endDate', new \DateTime()),
new AndX(
new IsNull('endDate'),
new LessThan('startDate', new \DateTime('-4weeks'))
)
)
]);
// Note: Advertisement repository is an instance that uses the SpecificationRepositoryTrait
return $this->em->getRepository('Advertisement')->match($spec)->execute();
use Entity\Advertisement;
class ExpiredAds extends Specification
{
public function __construct()
{
$specs = [
new Equals('ended', 0),
new OrX(
new LessThan('endDate', new \DateTime()),
new AndX(
new IsNull('endDate'),
new LessThan('startDate', new \DateTime('-4weeks'))
)
)
];
parent::__construct($specs);
}
public function isSatisfiedBy($value)
{
return $value === Advertisement::class;
}
}
use Entity\User;
class AdsByUser extends Specification
{
public function __construct(User $user)
{
$specs = [
new Select('u'),
new Join('user', 'u'),
new Equals('id', $user->getId(), 'u'),
];
parent::__construct($specs);
}
public function isSatisfiedBy($value)
{
return $value == Advertisement::class && parent::isSatisfiedBy($value);
}
}
class SomeService
{
/**
* Fetch Adverts that we should close but only for a specific company
*/
public function myQuery(User $user)
{
$spec = new Specification([
new ExpiredAds(),
new AdsByUser($user),
]);
return $this->em->getRepository('Advertisement')->match($spec)->execute();
}
/**
* Fetch adverts paginated by Doctrine Paginator with joins intact.
* A paginator can be iterated over like a normal array or Doctrine Collection
*/
public function myPaginatedQuery(User $user, $page = 1, $size = 10)
{
$spec = new Specification([
new ExpiredAds(),
new AdsByUser($user),
]);
$query = $this->em->getRepository('Advertisement')->match($spec);
$query->setFirstResult(($page - 1) * $size))
->setMaxResults($size);
return new Paginator($query);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.