Symfony2 直接SQLを実行


use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Setup; 

//とりあえず、たくさんインポート  中略    
$em = $this->getDoctrine()->getEntityManager();  
$rsm = new ResultSetMapping();  
$rsm->addEntityResult('Acme\HelloBundle\Entity\Post', 'p');  
$rsm->addFieldResult('p', 'id', 'id');
$rsm->addFieldResult('p', 'name', 'name');  
$query = $em->createNativeQuery('SELECT id, name FROM Post WHERE id = ?', $rsm);  
$query->setParameter(1, 123);  
$resulet =  $query->getArrayResult();//getArrayResult()は配列で取得    
//getResult() もあるがなんだかよくわからない。




$stmt = $this->getDoctrine()->getEntityManager()->getConnection()->prepare('select * from mimicry_ranking');
$result = $stmt->fetchAll();//どうやってバインドするかは不明




Four o four - Doctrine - PHP Database Tools
The Doctrine Project is the home to several PHP libraries primarily focused on database storage and object mapping. The core projects are the Object Relational Mapper (ORM) and the Database Abstraction Layer (DBAL) it is built upon.