1

家庭实体与地址实体具有一对多的关系。在 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
    ...
    }
4

1 回答 1

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。

于 2013-08-14T23:57:20.177 回答