我已經創建了兩個類Website
和WebsiteDomain
。可以有多個域到一個網站,所以我已經在類中的註釋中建立了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串行陣列反序列化。
任何指針都不錯地接收。
'private $ website'是否缺少'joinColumn'?還是應該默認?查看其他Doctrine代碼,我明白它......'* @ORM \ JoinColumn(name =「website_id」,referencedColumnName =「id」)'...... – ficuscr
您的模型是否有訪問器方法?我對JMS不熟悉,它是否使這些不必要的不必要? –
最好我可以告訴[JMS](https://jmsyst.com/libs/serializer)這裏只是一個序列化程序,並且使用的註釋是'@ type'。認爲這就是魔術的範圍。用於身份映射和緩存?好奇,現在。 – ficuscr