這裏有一些上下文信息:我正在用Doctrine2和FOSRestBundle構建一個Symfony2應用程序。Doctrine2級聯創建
我的問題:我想能夠創建一個家長與他的孩子只有一個JSON和一個數據庫訪問。
我的JSON是這樣的:
{
"name": "TEST_NAME",
"info": "TEST_INFO",
"cmts": [
{
"cmt": "CMT1",
"info": "INFO1"
},
{
"cmt": "CMT2",
"info": "INFO2"
},
{
"cmt": "CMT3",
"info": "INFO3"
}
]
}
這裏是我的測試實體:
<?php
namespace App\Bundle\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Test
*
* @ORM\Table(name="Test")
* @ORM\Entity(repositoryClass="App\Bundle\DemoBundle\Entity\TestRepository")
*/
class Test
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Assert\NotBlank()
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="info", type="string", length=255, nullable=true)
*/
private $info;
/**
* @ORM\OneToMany(targetEntity="TestCmt", mappedBy="test", fetch="EAGER", orphanRemoval=true, cascade={"merge", "remove", "persist"})
*/
protected $cmts;
/**
* Constructor
*/
public function __construct()
{
$this->cmts = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add cmts
*
* @param \App\Bundle\DemoBundle\Entity\TestCmt $cmts
* @return Test
*/
public function addCmt(\App\Bundle\DemoBundle\Entity\TestCmt $cmts)
{
$this->cmts[] = $cmts;
return $this;
}
/**
* Remove cmts
*
* @param \App\Bundle\DemoBundle\Entity\TestCmt $cmts
*/
public function removeCmt(\App\Bundle\DemoBundle\Entity\TestCmt $cmts)
{
$this->cmts->removeElement($cmts);
}
/**
* Get cmts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCmts()
{
return $this->cmts;
}
// other getters/setters...
}
而且我TESTCMT實體:
<?php
namespace App\Bundle\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TestCmt
*
* @ORM\Table(name="TestCmt")
* @ORM\Entity(repositoryClass="App\Bundle\DemoBundle\Entity\TestCmtRepository")
*/
class TestCmt
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="cmt", type="string", length=255)
*/
private $cmt;
/**
* @var string
*
* @ORM\Column(name="info", type="string", length=255, nullable=true)
*/
private $info;
/**
* @var Test
*
* @ORM\ManyToOne(targetEntity="Test", inversedBy="cmts")
* @ORM\JoinColumn(name="test_id", referencedColumnName="id")
**/
private $test;
/**
* Set test
*
* @param \App\Bundle\DemoBundle\Entity\Test $test
* @return TestCmt
*/
public function setTest(\App\Bundle\DemoBundle\Entity\Test $test = null)
{
$this->test = $test;
return $this;
}
/**
* Get test
*
* @return \App\Bundle\DemoBundle\Entity\Test
*/
public function getTest()
{
return $this->test;
}
}
而且finaly我postTestAction():
public function postTestAction(Request $request)
{
$entity = $this->deserialize($request, 'App\DemoBundle\Entity\Test');
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $entity;
}
當我發送JSON時,會創建TEST和TESTCMTs。儘管如此,創建的TESTCMT中的所有「test_id」都是「null」...這就是我的問題!
編輯:使用SQL Server事件探查器,我可以看到,教義做出處理SQL請求:
INSERT INTO TESTCMT (test_id, cmt, info) VALUES (null, 'CMT', 'INFO')
我不知道爲什麼學說不能發送爲test_id ...... TEST被TESTCMT之前創建,因此Doctrine可以訪問「test_id」來創建關聯的TESTCMT。
有人可以幫我解決嗎? :)
一個新的ID,請,我真的需要知道如何給全自動FK到兒童與級聯= {「堅持」,「合併」}! –