PHP 原则 - 无法按顺序对 2 个表执行刷新



我正在使用Doctrine 2和Symfony 2.7。它在我的本地机器上运行良好,但在生产中遇到了奇怪的问题。

我有一个PHP脚本,可以创建新的用户实体,然后为该用户创建新的用户配置文件实体。但是有时创建用户配置文件,有时不创建。更糟糕的是,脚本的其余部分也停止了。

这是我的代码

$user = new EntityUser();
$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 EntityUserProfile();
$profile->setIdUser($user->getId());
$profile->setIdDomain($themeHelper->getIdDomain());
$profile->setIsActive(true);
$profile->setIdAccountType($data['idAccountType']);
$em->persist($profile);
$em->flush();

我是否必须等待用户上的"$em->flush((;"完成,而不是继续执行其余部分?

非常感谢。

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 EntityUserProfile();
$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

最新更新