8

有没有一种方法可以在 symfony2 中使用实体管理器(原则)手动指定事务,或者可能是在单个事务中完成我在下面两个中所做的自然方式?

// creating screen object...
//Creating user object...

        //flush the screen into database in order to get the Id to relate the server (user) to
        $em->persist($screen);
        $em->flush();

        //Get id of just inserted screen and attach that to new server (user)
        $tempRecordId = $screen->getId();
        $tempEntity = $em->getRepository('BizTVContainerManagementBundle:Container')->find($tempRecordId);
        $entity->setScreen($tempEntity);

        //Flush the user also into database
        $em->persist($entity);
        $em->flush();

请参阅我必须刷新我的第一个实体才能获得它的 ID,这样我就可以将我的第二个实体与我的第一个实体联系起来......

4

2 回答 2

23
try {
    $em->getConnection()->beginTransaction();

    // do your thing here

    $em->getConnection()->commit();
} catch (\Exception $e) {
    $em->getConnection()->rollback();
    throw $e;
}
于 2012-08-17T07:20:45.937 回答
7

你为什么不这样做:

// creating screen object...
//Creating user object...
    $entity->setScreen($screen);
    $em->persist($screen);
    $em->persist($entity);
    $em->flush();
于 2012-08-16T22:24:05.427 回答