这里有一些上下文信息:我正在用Doctrine2和FOSRestBundle构建一个Symfony2应用程序。
我的问题:我希望能够通过一个JSON和一个数据库访问权限为子女创建一个父级。
我的JSON看起来像这样:
{
"name": "TEST_NAME",
"info": "TEST_INFO",
"cmts": [
{
"cmt": "CMT1",
"info": "INFO1"
},
{
"cmt": "CMT2",
"info": "INFO2"
},
{
"cmt": "CMT3",
"info": "INFO3"
}
]
}
这是我的TEST实体:
<?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;
}
}
最后我的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和TESTCMT。尽管如此,创建的TESTCMT中的所有“test_id”都是“空”......这就是我的问题!
编辑:使用SQL Server Profiler,我可以看到Doctrine发出了Transact SQL请求:
INSERT INTO TESTCMT (test_id, cmt, info) VALUES (null, 'CMT', 'INFO')
我不知道为什么Doctrine无法发送test_id ... TEST是在TESTCMT之前创建的,因此Doctrine应该可以访问“test_id”以创建关联TESTCMT。
有人可以帮我解决吗? :)
答案 0 :(得分:0)
删除@ORM \ GeneratedValue(strategy =“AUTO”),它不会让数据库为实体生成新的ID