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; }