我在 Symfony2 和学说中有一个小项目,我正在尝试更新 2 个相关实体:
会员和汽车
$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);
}
成员
/**
* @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;