学说删除拥有方多对多关系
Doctrine remove owning side Many To Many relation
我在 Symfony 3 中的 2 个实体之间有这种关系:
class Project
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Neighborhood", inversedBy="projects")
* @ORM\JoinTable(name="neighborhood_project")
*/
private $neighborhoods;
public function __construct() {
$this->neighborhoods = new ArrayCollection;
}
public function getNeighborhoods()
{...}
public function setNeighborhoods(array $entities)
{...}
public function addNeighborhood(Neighborhood $entity)
{...}
public function removeNeighborhood(Neighborhood $entity)
{...}
}
class Neighborhood
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false, unique=true)
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Project", mappedBy="neighborhoods")
*/
private $projects;
public function __construct() {
$this->projects = new ArrayCollection;
}
public function getProjects()
{...}
public function setProjects(array $entities)
{...}
public function addProject(Project $entity)
{...}
public function removeProject(Project $entity)
{...}
}
这很好用。但是我遇到的问题是删除关系。
$em->remove($neighborhood); 删除 table 'neighborhood' 中的邻域和所有table'neighborhood_project'.
中的关系
$em->remove($project);删除table'project'中的项目和不删除table'neighborhood_project'中的关系。
我尝试了很多方法来删除拥有方的关系,但没有找到任何解决方案。
如何删除删除拥有方的关系?
已添加:
级联:
@ORM\ManyToMany(targetEntity="Neighborhood", inversedBy="projects", cascade={"remove"})
无济于事。这将删除实体、关系和相关实体。
我需要相同的功能,例如 $em->remove($neighborhood);
我有,我的错。
我应该只更新数据库中的架构 (doctrine:schema:update)。
我在 Symfony 3 中的 2 个实体之间有这种关系:
class Project
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Neighborhood", inversedBy="projects")
* @ORM\JoinTable(name="neighborhood_project")
*/
private $neighborhoods;
public function __construct() {
$this->neighborhoods = new ArrayCollection;
}
public function getNeighborhoods()
{...}
public function setNeighborhoods(array $entities)
{...}
public function addNeighborhood(Neighborhood $entity)
{...}
public function removeNeighborhood(Neighborhood $entity)
{...}
}
class Neighborhood
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false, unique=true)
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Project", mappedBy="neighborhoods")
*/
private $projects;
public function __construct() {
$this->projects = new ArrayCollection;
}
public function getProjects()
{...}
public function setProjects(array $entities)
{...}
public function addProject(Project $entity)
{...}
public function removeProject(Project $entity)
{...}
}
这很好用。但是我遇到的问题是删除关系。
$em->remove($neighborhood); 删除 table 'neighborhood' 中的邻域和所有table'neighborhood_project'.
中的关系$em->remove($project);删除table'project'中的项目和不删除table'neighborhood_project'中的关系。
我尝试了很多方法来删除拥有方的关系,但没有找到任何解决方案。 如何删除删除拥有方的关系?
已添加:
级联:
@ORM\ManyToMany(targetEntity="Neighborhood", inversedBy="projects", cascade={"remove"})
无济于事。这将删除实体、关系和相关实体。 我需要相同的功能,例如 $em->remove($neighborhood);
我有,我的错。 我应该只更新数据库中的架构 (doctrine:schema:update)。