为什么在Doctrine 2 One to Many关系中,反面的ID不会被添加到拥有方?

时间:2017-09-14 20:40:39

标签: php doctrine-orm silex

我创建了两个类WebsiteWebsiteDomain。网站可以有多个域,因此我在类的注释中设置了OneToMany关系。

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

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;

}

现在,当我创建一个附有多个域的新网站时,所有记录都在相关表中创建,但域所属的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       |

website_id在与第一个表格中的网站相关的最后一个表格中应该为空。

我知道这个问题在这里已被多次询问,但我无法找到答案。我玩过不同的PDO驱动程序,SQL和MySQL,两者都表现出同样的问题。

我使用以下对象创建记录。我唯一能想到的是,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)
}

使用JMS Serializer从数组中反序列化此对象。

任何指针都很受欢迎。

1 个答案:

答案 0 :(得分:0)

通常在使用Doctrine时,我发现当我忘记在父{q} sorted_dates = dates.sort_by { |date| (date - test_date).abs } 方法中设置子实体的父级时,就会发生这种情况。

特别针对您的addChild实体的addDomain方法中的示例。默认情况下,它将是这样的:

Website

但您需要在该方法中明确设置父级。

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

级联注释似乎会自动执行此操作,但它不会。

我不确定您的设置是否有必要。这可能不是你遇到的同样问题,因为我不熟悉你正在使用的一些工具,但认为值得一试。

相关问题