2017-09-14 58 views
2

我已經創建了兩個類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

回答

0

通常在使用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

謝謝你,這是有道理的。我會嘗試一下,看看我能想出什麼。 –

相關問題