I am working on the symfony 2.5 project with doctrine 2.4.
I want to cache query result with cache id and cache time, so I can delete the cache result, whenever needed though admin.
I am able to cache the query result with "createQueryBuilder()" option.
Example:
$this->createQueryBuilder('some_table')
->select('some_table')
->where('some_table.deleted_date IS NULL')
->getQuery()
->useResultCache(true)
->setResultCacheLifetime(120) //Query Cache lifetime
->setResultCacheId($queryCacheId) //Query Cache Id
->getResult();
But I am not able to find the similar way to chache query result for "findOneBy()" option.
Example:
$this->findOneBy(array('some_field'=>$some_value));
I am looking some proper solution, any help is much appreciated.
You need to redefine every findBy* or findOneBy* function into custom repository: this is the only way as doctrine2 default behaviour doesn't take into account this situation. Is up to you, unfortunately.
Also Ocramius (a Doctrine2 devel) say it here https://groups.google.com/d/msg/doctrine-user/RIeH8ZkKyEY/HnR7h2p0lCQJ
Make a common function for this.
$repo->findOneByYourSufff($yourStuff, $yourRepoClass);
Followed by your common function :
public function findOneByYourSufff($yourStuff, $yourRepoClass) {
$q = $this->createQueryBuilder()
->select('x.*')
->from($yourRepoClass, 'x');
foreach($yourStuff as $fieldKey => $wh) {
$q->andWhere("b.$fieldKey = :fieldName");
$q->setParameter("fieldName", $wh);
}
$q->useResultCache(true)
->setResultCacheLifetime(120) //Query Cache lifetime
->setResultCacheId($queryCacheId) //Query Cache Id
->getSingleResult();
return $q
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With