Skip to content

Instantly share code, notes, and snippets.

@liuggio
Created July 9, 2012 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuggio/3075742 to your computer and use it in GitHub Desktop.
Save liuggio/3075742 to your computer and use it in GitHub Desktop.
doctrine Centralized query RFC

Now:

 $query = $this->getEntityManager()
            ->createQuery(
            'SELECT s FROM AcmeDemoBundle:Service s
             WHERE  s.status = :serviceStatus
            ');
        $query->setParameter('serviceStatus', Service::STATUS_ENABLED);

        $query->useResultCache(true, 10, 'findActiveServices');

The idea is centralize the cache mechanism and the query creation, into the configuration file store the cache mechanism and the dql.

RFC configuration file example yaml

#'Acme\Bundle\CityTour\ProductBundle\Repository\CityTourRepository\findActiveCTServices':
findActiveCTServices:
  resultLifetime: 10
  resultCache: true
  queryCache: false
  dql:'SELECT s
        FROM AcmeDemoBundle:Service s
        WHERE s.status = :serviceStatus'

And the repository will be:

 $query = $this->getEntityManager()
            ->createQueryByIdentifier('findActiveCTServices');
           
        $query->setParameter('serviceStatus', ProductStatus::ENABLED);
        $query->setParameter('serviceTypeCode', 'CT');
        // implicit!!! $query->useResultCache(true, 10, 'findActiveCTServices'); 

the ResultCacheId if the parameters are not empty will be something like 'findActiveCTServices' .'__' . serialize($query->getParameters())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment