为什么在主体2一对多关系中,反面的ID不会被添加到拥有的一面?

问题描述:

我已经创建了两个类WebsiteWebsiteDomain。可以有多个域到一个网站,所以我已经在类中的注释中建立了OneToMany关系。为什么在主体2一对多关系中,反面的ID不会被添加到拥有的一面?

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串行阵列反序列化。

任何指针都不错地接收。

+0

'private $ website'是否缺少'joinColumn'?还是应该默认?查看其他Doctrine代码,我明白它......'* @ORM \ JoinColumn(name =“website_id”,referencedColumnName =“id”)'...... – ficuscr

+0

您的模型是否有访问器方法?我对JMS不熟悉,它是否使这些不必要的不​​必要? –

+0

最好我可以告诉[JMS](https://jmsyst.com/libs/serializer)这里只是一个序列化程序,并且使用的注释是'@ type'。认为这就是魔术的范围。用于身份映射和缓存?好奇,现在。 – ficuscr

通常在使用Doctrine时,我发现当我忘记在父级的addChild方法中设置子实体的父级时会发生这种情况。

特别为你的例子,将在Website实体的addDomain方法。默认情况下,它会是这样的:

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

但是你需要显式地设置父方法。

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

它可能看起来像级联注释会自动执行此操作,但它不会。

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

+0

谢谢你,这是有道理的。我会尝试一下,看看我能想出什么。 –