[英]Symfony2 and Doctrine: One to Many relationship
I am writing an application in Symfony2 and Doctrine. 我正在Symfony2和Doctrine中编写一个应用程序。 Here's all the codes that might be necessary:
以下是可能需要的所有代码:
https://gist.github.com/3440325 https://gist.github.com/3440325
This code block works fine and creates the relationship correctly: 此代码块工作正常并正确创建关系:
$twitter->setUser($user);
$skype->setUser($user);
Working correctly means it creates a row in the users table and inserts the correct user id in the handles table. 正确工作意味着它在users表中创建一行,并在handles表中插入正确的用户ID。
Where as, this code block doesn't work as expected: 在哪里,此代码块不能按预期工作:
$user->addHandle($skype);
$user->addHandle($twitter);
It successfully inserts all the entries but can't insert the correct user id in the handles table. 它成功插入了所有条目,但无法在handle表中插入正确的用户ID。 As a matter of fact, the user_id column remains empty.
事实上,user_id列仍为空。
What is going wrong here? 这里出了什么问题? Am I missing something?
我错过了什么吗? Is my expectation incorrect or there is some bug some where?
我的期望是不正确还是有些错误在某些地方?
-- Masnun - Masnun
Since you have a bidirectional one-to-many relationship, you need to set referenced entities on both sides synchronously. 由于您具有双向一对多关系,因此需要同步设置两侧的引用实体。
public function addHandle(\WeCodePHP\HomeBundle\Entity\Handle $handles)
{
$this->handles[] = $handles;
$handles->setUser($this);
}
Otherwise doctrine won't guess what a handle belongs to. 否则,学说将不会猜测句柄属于什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.