[英]How do I save associated entities in Doctrine 2?
After a few hours of searching for an answer I have to ask this in StackOverflow. 在搜索了几个小时后,我不得不在StackOverflow中问这个问题。 This seems such a basic thing to do but I'm still quite new to Doctrine2 so forgive me if the answer is simple or if I'm totally looking at Doctrine incorrectly. 这似乎是一件很基本的事情,但是我对Doctrine2还是很陌生,所以如果答案很简单或者我完全错误地看待Doctrine,请原谅我。
My problem is that my foreign keys are not being populated. 我的问题是我的外键没有被填充。 I was assuming Doctrine would do it automatically. 我以为Doctrine将自动执行此操作。
For example, I have two entities: Name
and EmailAddress
with a OneToMany relationship. 例如,我有两个实体: Name
和EmailAddress
一个一对多的关系。 Name
has many EmailAddress
and EmailAddress
has one Name
. Name
有许多EmailAddress
而EmailAddress
只有一个Name
。
Here's pre-populated tables. 这是预先填充的表格。
Table: name Table: email_address
| id | name | | id | email_address | name_id |
|----|------| |----|-------------------|---------|
| 1 | Luke | | 1 | luke@starwars.com | 1 |
First, I tried just saving into EmailAddress
: 首先,我尝试仅保存到EmailAddress
:
<?php
$newEmail = new Foo\Entities\Emails();
$newEmail->setEmailAddress('luke.skywalker@starwars.com');
$em->persist($newEmail);
$em->flush();
$em->clear();
The email_address
field is saved, but the foreign key (name_id) is always blank. email_address
字段已保存,但外键(name_id)始终为空。
I also tried: 我也尝试过:
<?php
$newEmail = new Foo\Entities\Emails();
$newEmail->setEmailAddress('luke.skywalker@starwars.com');
$newName = $em->getRepository('Foo\Entities\Names')->findOneBy(['id' => 1]);
$newName->addEmail($newEmail);
$em->persist($newName);
$em->flush();
$em->clear();
Same result, foreign key is ignored. 结果相同,外键被忽略。
Here are my entities: 这是我的实体:
Name 名称
/**
* @namespace
*/
namespace Foo\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Foo\Entities\Emails;
/**
* @ORM\Entity
* @ORM\Table(name="names")
*/
class Names
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=50)
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="Emails", mappedBy="names", cascade={"persist"})
* @ORM\JoinColumn(name="id", referencedColumnName="name_id")
*/
private $emails;
public function __construct()
{
$this->emails = new ArrayCollection();
}
public function setId($value)
{
$this->id = $value;
}
public function getId()
{
return $this->id;
}
public function setName($value)
{
$this->name = $value;
}
public function getName()
{
return $this->name;
}
public function addEmail(Emails $email)
{
$this->emails->add($email);
}
public function getEmails()
{
return $this->emails;
}
}
Emails 电子邮件
/**
* @namespace
*/
namespace Foo\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="emails")
*/
class Emails
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=50)
*/
protected $email_address;
/**
* @ORM\Column(type="integer")
*/
protected $name_id;
/**
* @ORM\ManyToOne(targetEntity="Names", inversedBy="emails")
* @ORM\JoinColumn(name="name_id", referencedColumnName="id")
*/
private $names;
public function setId($value)
{
$this->id = $value;
}
public function getId()
{
return $this->id;
}
public function setEmailAddress($value)
{
$this->email_address = $value;
}
public function getEmailAddress()
{
return $this->email_address;
}
public function setNameId($value)
{
$this->name_id = $value;
}
public function getNameId()
{
return $this->name_id;
}
}
So I finally figured it out. 所以我终于想通了。 I should have been setting Names
to Emails
. 我应该已经将Names
设置为Emails
。
Modified Emails
entity: 修改后的Emails
实体:
class Emails
{
...
public function setName(Foo\Entities\Names $name)
{
$this->names = $name;
}
}
And then: 接着:
$name = $em->getRepository('Foo\Entities\Names')->findOneBy(['id' => 1]);
$newEmail = new Foo\Entities\Emails();
$newEmail->setEmailAddress('luke.skywalker@starwars.com');
$newEmail->setName($name);
$em->persist($newEmail);
$em->flush();
$em->clear();
Sets the foreign key of new email address record. 设置新电子邮件地址记录的外键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.