11.Doctrine2 (3)

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;

http://symfony.com/doc/current/components/var_dumper.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值