家庭实体与地址实体具有一对多的关系。在 Netbeans 调试中逐步执行编辑操作,我可以在请求对象中看到地址,但在绑定请求后地址消失(快照 = 数组 [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
...
}