正文  软件开发 > PHP编程技术 >

symfony 事务提交,symfony提交

symfony 事务提交,symfony提交1. 添加数据 新建一个对象,给对象赋值 $em = $this->getDoctrine()->getManager(); //添加事物 $em-...

symfony 事务提交,symfony提交

1. 添加数据

新建一个对象,给对象赋值

$em = $this->getDoctrine()->getManager(); //添加事物
$em->getConnection()->beginTransaction(); 
try { 
  $model= new class();
  $model->setName($name);
  $em->persist($model );
  $em->flush(); 
  $em->getConnection()->commit(); 
} catch (Exception $ex) {
  $em->getConnection()->rollback();
  throw $ex;
}

  

2. 修改数据

Bundle:table
Bundle名字,和table名
$Obj = $this->getDoctrine()->getRepository(‘Bundle:table‘)->findOneBy(  array(‘id‘=>$id));
$em = $this->getDoctrine()->getManager(); //添加事物 $em->getConnection()->beginTransaction(); try { $Obj->setName( $name); $em->persist($Obj); $em->flush(); $em->getConnection()->commit(); } catch (Exception $ex) { $em->getConnection()->rollback(); throw $ex; }

  

3. 删除

$Obj = $this->getDoctrine()->getRepository(‘Bundle:table‘)->findOneBy(  array(‘id‘=>$id));
$em = $this->getDoctrine()->getManager(); //添加事物
        $em->getConnection()->beginTransaction();        
        try {
            $em->remove($Obj);
            $em->flush();            
            $em->getConnection()->commit(); 
        } catch (Exception $ex) {
            $em->getConnection()->rollback();
            throw $ex;
        }

  

http://www.bkjia.com/PHPjc/1227996.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1227996.htmlTechArticlesymfony 事务提交,symfony提交 1. 添加数据 新建一个对象,给对象赋值 $em = $this-getDoctrine()-getManager(); //添加事物$em-getConnection()-beginTransaction...