gpt4 book ai didi

php - Symfony2 & Doctrine2 : removeElement doesn't work

转载 作者:行者123 更新时间:2023-12-04 20:41:59 24 4
gpt4 key购买 nike

我在 Symfony2 和学说中有一个小项目,我正在尝试更新 2 个相关实体:

成员(member)和汽车

$carMembers = $car->getMembers();
echo count($carMembers); // --> show 2
echo get_class(carMembers[0]); // --> show MyCars\WSBundle\Entity\Member
$car->removeMember($member);
$em->persist($car);
$em->flush();

$carMembers= $car->getMembers();
echo count($carMembers); // --> show 1
echo get_class(carMembers[0]); // --> show MyCars\WSBundle\CarsController !!!

有我的实体:

汽车
/**
* @ORM\ManyToMany(targetEntity="Member", mappedBy="cars")
*/
private $members;

/**
* Remove Member
*
* @param MyCars\WSBundle\Entity\Member $member
*/
public function removeMember(\MyCars\WSBundle\Entity\Member $member)
{
$this->members->removeElement($member);
$member->removeCar($this);
}

成员(member)
/**
* @ORM\ManyToMany(targetEntity="Car", cascade={"persist"})
* @ORM\JoinTable(name="cars_membres",
* joinColumns={@ORM\JoinColumn(name="member_id", referencedColumnName="member_id")},
* inverseJoinColumns={@ORM\JoinColumn(name="car_id", referencedColumnName="car_id")}
* )
*/
private $cars;

最佳答案

我想你要找的是orphanRemoval关系选项。

@ORM\ManyToMany(targetEntity="Car", cascade={"persist"}, orphanRemoval=true)

因此,当您从集合中删除项目并刷新实体管理器时,它将从数据库中删除关系记录...

关于php - Symfony2 & Doctrine2 : removeElement doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10465474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com