PHP Doctrine - 无法按顺序对2个表执行Flush

时间:2017-07-14 10:16:03

标签: php symfony doctrine-orm doctrine

我正在使用Doctrine 2和Symfony 2.7。它在我的本地机器上工作正常,但在生产时遇到了奇怪的问题。

我有一个PHP脚本,可以创建新的User实体,然后为该用户创建新的UserProfile实体。但有时会创建UserProfile,有时则不会。更糟糕的是,脚本的其余部分也会停止运行。

这是我的代码

$user = new Entity\User();
$user->setEmail(trim($data['email']));
$user->setName(trim($data['email']));
$user->setSalt($userManager->generateSalt());
$user->setPassword($data['encrypted_password']);
$em->persist($user);
$em->flush();

$profile = new Entity\UserProfile();
$profile->setIdUser($user->getId());
$profile->setIdDomain($themeHelper->getIdDomain());
$profile->setIsActive(true);
$profile->setIdAccountType($data['idAccountType']);
$em->persist($profile);
$em->flush();

我是否有机会等待“$ em-> flush();”在用户完成,而不是继续执行其余的?

非常感谢。

1 个答案:

答案 0 :(得分:0)

Try once :
After flushing the $user object first check the user record has been set properly and after checking it in 'if' condition like:
 if($user-> get Id())
{
$profile = new Entity\UserProfile();
$profile->setIdUser($user->getId());
$profile->setIdDomain($themeHelper->getIdDomain());
$profile->setIsActive(true);
$profile->setIdAccountType($data['idAccountType']);
$em->persist($profile);
$em->flush();
}
may be this will work and if not you would be able to know where it is creating problem