家庭实体与地址实体具有一对多的关系。单步执行Netbeans调试中的编辑操作,我可以看到请求对象中的地址,但在请求绑定后,地址消失(snapshot = array [0])。所以没有地址可以保留。
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Mana\ClientBundle\Entity\Address", mappedBy="household",cascade={"persist"})
*/
private $addresses;
/**
* Add addresses
*
* @param \Mana\ClientBundle\Entity\Address $addresses
* @return Household
*/
public function addAddress(Address $address) {
$this->addresses[] = $address;
return $this;
}
/**
* @var \Mana\ClientBundle\Entity\Household
*
* @ORM\ManyToOne(targetEntity="Mana\ClientBundle\Entity\Household", inversedBy="addresses")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="household_id", referencedColumnName="id")
* })
*/
private $household;
/**
* Set household
*
* @param \Mana\ClientBundle\Entity\Household $household
* @return Address
*/
public function setHousehold(\Mana\ClientBundle\Entity\Household $household = null)
{
$this->household = $household;
return $this;
}
public function updateAction($id, Request $request) {
$em = $this->getDoctrine()->getManager();
$household = $em->getRepository('ManaClientBundle:Household')->find($id);
if (!$household) {
throw $this->createNotFoundException('Unable to find Household.');
}
$form = $this->createForm(new HouseholdType(), $household);
$form->bind($request); // address in $request
if ($form->isValid()) {
$members = $household->getMembers(); // this has members
$addresses = $household->getAddresses(); // this has no addresses
...
}
答案 0 :(得分:1)
因此,根据您的评论,您想要更改:
public function addAddress(Address $address) {
$this->addresses[] = $address;
return $this;
}
要
public function addAddress(Address $address) {
$this->addresses[] = $address;
$address->setHousehold($this);
return $this;
}
这将确保您的链接已设置。
仍然有点令人费解,因为听起来你在发布一个新地址后在你的控制器中测试了getAddresses。