简体   繁体   English

为什么在教义2一对多关系中不将反面的ID添加到拥有方?

[英]Why is the ID of the inverse side not being to added to the owning side in Doctrine 2 One to Many relationship?

I have created two classes Website and WebsiteDomain . 我创建了两个类WebsiteWebsiteDomain There can be multiple domains to a website so I have set up a OneToMany relationship in the annotations in the classes. 一个网站可以有多个域,因此我在类的注释中设置了OneToMany关系。

Website.php Website.php

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * Website object for the chosen site
 *
 * @ORM\Entity
 * @ORM\Table(name="websites")
 */
class Website
{

    /**
     * @JMS\Type("integer")
     *
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\GeneratedValue
     *
     * @var integer $id
     */
    protected $id;

    /**
     * Domains that this website answers on
     *
     * @JMS\Type("ArrayCollection<Turtle\Model\Entity\WebsiteDomain>")
     * @ORM\OneToMany(targetEntity="WebsiteDomain", mappedBy="website", cascade={"persist", "remove"})
     *
     * @var WebsiteDomain
     */
    private $domains;

}

WebsiteDomain.php WebsiteDomain.php

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * Website object for the chosen site
 *
 * @ORM\Entity
 * @ORM\Table(name="website_domains")
 */
class WebsiteDomain
{

    /**
     * @JMS\Type("integer")
     *
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\GeneratedValue
     *
     * @var integer $id
     */
    protected $id;

    /**
     * Website that this domain belongs to
     *
     * @JMS\Type("Website")
     * @ORM\ManyToOne(targetEntity="Website", inversedBy="domains")
     *
     * @var \Turtle\Model\Entity\Website
     */
    private $website;

}

Now when I create a new Website that has multiple domains attached to it all the records are created in the relevant tables, but the webiste_id that the domains belong to is NULL. 现在,当我创建一个具有多个域的新网站时,所有记录都将在相关表中创建,但是这些域所属的webiste_id为NULL。

| id | name    | description    | parent | storageName |
|----|---------|----------------|--------|-------------|
| 1  | foo_bar | FooBar Website |        | foo_bar     |

| id | website_id | domain       | primary | 
|----|------------|--------------|---------|
| 1  | NULL       | foobar.co.uk | 1       |

The website_id should be null in the last table relating to the website in the first table. 与第一个表中的网站相关的最后一个表中的website_id应该为null。

I know this question has been asked many times on here but I have not be able to find an answer. 我知道这个问题已经在这里被问过很多次了,但是我找不到答案。 I have played around with different PDO drivers, SQL and MySQL and both exhibit the same problem. 我玩过不同的PDO驱动程序,SQL和MySQL,并且都表现出相同的问题。

I am creating the records using the following object. 我正在使用以下对象创建记录。 The only thing I can think that is that the website_id in the WebsiteDomain is set to null, but if this is the case how can I get Doctrine to override this value? 我唯一能想到的是,将WebsiteDomain中的website_id设置为null,但是如果是这种情况,我如何获取Doctrine来覆盖此值?

object(Turtle\Model\Entity\Website)#412 (10) {
  ["name":"Turtle\Model\Entity\Website":private]=>
  string(7) "foo_bar"
  ["displayName":"Turtle\Model\Entity\Website":private]=>
  string(7) "Foo Bar"
  ["description":"Turtle\Model\Entity\Website":private]=>
  string(14) "FooBar Website"
  ["parent":"Turtle\Model\Entity\Website":private]=>
  NULL
  ["domains":"Turtle\Model\Entity\Website":private]=>
  object(Doctrine\Common\Collections\ArrayCollection)#410 (1) {
    ["elements":"Doctrine\Common\Collections\ArrayCollection":private]=>
    array(1) {
      [0]=>
      object(Turtle\Model\Entity\WebsiteDomain)#371 (7) {
        ["website":"Turtle\Model\Entity\WebsiteDomain":private]=>
        NULL
        ["domain":"Turtle\Model\Entity\WebsiteDomain":private]=>
        string(12) "foobar.co.uk"
        ["primary":"Turtle\Model\Entity\WebsiteDomain":private]=>
        bool(true)
        ["id":protected]=>
        NULL
        ["created":protected]=>
        NULL
        ["modified":protected]=>
        NULL
        ["admupdated":protected]=>
        bool(false)
      }
    }
  }
  ["storageName":"Turtle\Model\Entity\Website":private]=>
  string(7) "foo_bar"
  ["id":protected]=>
  NULL
  ["created":protected]=>
  NULL
  ["modified":protected]=>
  NULL
  ["admupdated":protected]=>
  bool(false)
}

This object is being deserialized from an array using the JMS Serializer. 使用JMS序列化器从数组中反序列化此对象。

Any pointers are gratfully recieved. 任何指针都可以收到。

Normally when working with Doctrine, I've seen this happen when I've forgotten to set the parent on the child entity in the parent's addChild method. 通常,在使用Doctrine时,我已经忘记了在父项的addChild方法中将子项设置为子项实体时发生这种情况。

Specifically for your example that would be in the addDomain method of your Website entity. 专门针对您的示例,该示例将在您的Website实体的addDomain方法中。 By default, it will be something like this: 默认情况下,它将是这样的:

public function addDomain (WebsiteDomain $domain) {
     $this->domains[] = $domain;
     return $this;
}

But you need to explicitly set the parent in that method. 但是您需要在该方法中显式设置父级。

public function addDomain (WebsiteDomain $domain) {
     $domain->setWebsite($this);
     $this->domains[] = $domain;
     return $this;
}

It might seem like the cascade annotation would do this automatically, but it doesn't. 看起来级联注释会自动执行此操作,但不会。

I'm not sure if this is necessary in your set up. 我不确定在您的设置中是否需要这样做。 It might not be the same problem you're having, because I'm not familiar with some of the tools you're using, but figured it was worth a try. 这可能不是您遇到的相同问题,因为我不熟悉您使用的某些工具,但认为值得尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM