当前位置:Gxlcms > PHP教程 > symfony事务提交方法

symfony事务提交方法

时间:2021-07-01 10:21:17 帮助过:31人阅读

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('')->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;
        }

以上就是symfony 事务提交方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行