2017-07-14 32 views
0

我正在使用Doctrine 2和Symfony 2.7。它在我的本地機器上工作正常,但在生產中它遇到了一個奇怪的問題。PHP主義 - 無法在2表上按順序執行刷新

我有一個PHP腳本創建新的用戶實體,然後爲該用戶創建新的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();」在用戶完成,而不是繼續執行其餘的?

非常感謝。

+0

你會得到哪個錯誤? –

+1

這可能是也可能不是相關的,但這似乎不是必要的'$ profile-> setIdUser($ user-> getId());',應該有一個名爲' - > setUser($ user)'的方法。然後,在你堅持這兩個實體之後,你應該能夠調用' - > flush'一次,並且原則將負責映射和更正插入順序。 – Flosculus

+0

我沒有錯誤。大多數情況下,最終用戶註冊時會發生這種情況。我試圖尋找日誌文件中的錯誤,但沒有運氣,也試圖讓它自己再次發生,但它工作正常。 – pthphap

回答

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