DoctrineExtensions:
https://github.com/Atlantic18/DoctrineExtensions
https://github.com/stof/StofDoctrineExtensionsBundle
1. 直接调用 SQL :
// 这样取出的结果是原生的 mysql 结果集
$result = $this->get('database_connection')->fetchAll("select * from book ");
2.手动控制事务
$em = $this->getDoctrine()->getManager();
//第1种写法:
$em->getConnection()->beginTrasaction();
try {
$book2 = $em->getRepository('WwwWebBundle:Book')->findOneBy(array('id'=>2));
$book2->setPrice(222);
$em->persist($book2);
$em->getConnection()->commit();
} catch (Exception $e) {
$em->getConnection()->rollback();
}
//第2种写法:匿名函数中所有的操作都为事务操作
$em->getConnection(function($em){
$book2 = $em->getRepository('WwwWebBundle:Book')->findOneBy(array('id'=>2));
$book2->setPrice(222);
$em->persist($book2);
});
3.使用完整的 DQL 进行查询优化
4.在 DQL 中使用 partial 查询只含有一部分数据的 Entity 对象
5.使用构造函数创建对象
6.如何在 symfony2 里面 var_dump 一个 Entity
$query = $this->getDoctrine()->getManager()
->createQuery("SELECT u FROM WwwWebBundle:User u");
\Doctrine\Common\Util\Debug::dump($query->getResult());die;