Search code examples
symfonydoctrineapc

how to apc case doctrine prepare query in symfony2


I used followed query in symfony2 i need to APC cache with doctrine how to use it... I tried follow apc cache but not got error.

$query =  $this->getDoctrine()->getEntityManager()->getConnection()->prepare(
        "SELECT  V.id,
            (CASE WHEN "DAY"      THEN DATE_ADD( NOW() , INTERVAL start_date DAY ) 
                  WHEN "MONTH"    THEN DATE_ADD( NOW() , INTERVAL start_date MONTH )
                  WHEN "YEAR"     THEN DATE_ADD( NOW() , INTERVAL start_date YEAR ) 
             END as startDate
            ) `enter code here`
        FROM table_name V

         WHERE V.id = :id 
         ORDER BY date ASC"
    );
 $query->bindValue('id', $id)
 $query->setResultCacheDriver(new ApcCache())
 $query->useResultCache(true, 300, 'testcache');
 $query->execute();   
 $results = $query->fetchAll(); 

Solution

  • use Doctrine\ORM\Query\ResultSetMapping;
    
    $oResultSetMapp = new ResultSetMapping;
    $oResultSetMapp->addScalarResult('id', 'id');
    $oResultSetMapp->addScalarResult('name', 'name');
    
    $sqlQuery = "SELECT T.id AS id, T.enable, T.name
        FROM table_name as T
        WHERE T.enable =  :enable";
    
    $query = $this->getDoctrine()->getEntityManager()
            ->createNativeQuery($sqlQuery, $oResultSetMapp)
            ->setParameter('enable', 1) 
            ->setResultCacheDriver(new ApcCache())
            ->useResultCache(true, 300, 'testcache');
    $query->getResult();
    

    Try this one, this will help you for sure.