PHP code example of igdr / doctrine-specification

1. Go to this page and download the library: Download igdr/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/ */

    

igdr / doctrine-specification example snippets


// Not using the lib
$qb = $this->em->getRepository('User')
    ->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 Igdr\DoctrineSpecification\Specification;

// Using the lib
$spec = Specification::expr()->andX(
    Specification::expr()->eq('ended', 0),
    Specification::expr()->orX(
        Specification::expr()->lt('endDate', new \DateTime()),
        Specification::expr()->andX(
            Specification::expr()->isNull('endDate'),
            Specification::expr()->lt('startDate', new \DateTime('-4weeks'))
        )
    )
);

return $this->em->getRepository('RecruitmentBundle:Advert')->match($spec);

/**
 * Class AdvertsWeShouldCloseSpecification
 */
class AdvertsWeShouldCloseSpecification extends Specification
{
    /**
     * {@inheritdoc}
     */
    public static function create()
    {
        return new self();
    }

    /**
     * @param ExpressionInterface $spec
     *
     * @return $this
     */
    public function applyWhere(ExpressionInterface $spec)
    {
        $this->andWhere($spec);

        return $this;
    }

    /**
     * @param \DateTime $startDate
     *
     * @return $this
     */
    public function applyTimeFilter(\DateTime $startDate)
    {
        $spec = self::expr()->andX(
            self::expr()->eq('ended', 0),
            self::expr()->orX(
                self::expr()->lt('endDate'),
                self::expr()->andX(
                    self::expr()->isNull('endDate'),
                    self::expr()->lt('startDate', $startDate)
                )
            )
        );

        $this->andWhere($spec);

        return $this;
    }
}

$spec = AdvertsWeShouldCloseSpecification::create()->applyTimeFilter(new \DateTime('-4weeks')); 

return $this->em->getRepository('RecruitmentBundle:Advert')->match($spec);

class AdvertRepository extends EntitySpecificationRepository
{
    protected function filterAdvertsWeShouldClose($qb)
    {
        return $qb
            ->andWhere('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'))
        ;
    }

    protected function filterOwnedByCompany($qb, Company $company)
    {
        return $qb
            ->join('company', 'c')
            ->andWhere('c.id = :company_id')
            ->setParameter('company_id', $company->getId())
        ;
    }

    public function myQuery(Company $company)
    {
        $qb = $this->em->getRepository('RecruitmentBundle:Advert')->createQueryBuilder('r');
        $this->filterAdvertsWeShouldClose($qb)
        $this->filterOwnedByCompany($qb, $company)

        return $qb->getQuery()->getResult();
    }
}