简体   繁体   English

如何在教义2中保存关联的实体?

[英]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. 例如,我有两个实体: NameEmailAddress一个一对多的关系。 Name has many EmailAddress and EmailAddress has one Name . Name有许多EmailAddressEmailAddress只有一个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.

相关问题 如何通过实体属性限制获取原则中的关联实体? - How do I limit fetching associated entities in doctrine by an entity property? 通过学说将结果作为数组(水合物数组)获取时,如何获取关联实体? - How can I fetch associated entities when fetching results as an array (hydrate array) via doctrine? 如何在Doctrine中对多个实体执行以下查询? - How to do the following Query in Doctrine with multiple entities? 如何在cakePHP中为关联模型保存多个字段? - How do I save multiple fields for an associated model in cakePHP? Doctrine2应自动合并关联的实体 - Doctrine2 should automatically merge associated entities Symfony / Doctrine:如何减少SELECT查询的数量? (多级关联实体; twig + jsonSerialize()) - Symfony/Doctrine: How to reduce the num of SELECT-queries? (Multi-level associated entities; twig + jsonSerialize()) 如何使用Doctrine2中的级联选项自动保持关联实体? - How to use the cascade option in Doctrine2 to have associated entities automatically persisted? 如何映射学说实体 - How to map doctrine entities 我真的需要自己使用教义2移动/编写实体吗? - Do I really need to move/write entities myself with Doctrine 2? 如何限制Doctrine2中的关联实体结果? - How can I limit an associated entity result in Doctrine2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM