[英]Doctrine remove owning side Many To Many relation
I have this relation between 2 Entities in Symfony 3: 我在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)
{...}
}
This works fine. 这很好。 But Problem that I have, is removing the relations. 但是我的问题是消除关系。
$em->remove($neighborhood); $ EM->删除($附近); delete the neighborhood in the table ' neighborhood ' and all relations in the table ' neighborhood_project '. 删除表中的“ 街区 ”,并在表“neighborhood_project”的所有关系附近。
$em->remove($project); $ EM->删除($项目); delete the project in the table ' project ' and DOES NOT delete the relation in the table ' neighborhood_project '. 删除表“ project ”中的项目 ,并且不删除表“ neighbor_project ”中的关系。
I have tried many options to delete the relation on the owning side and didn't found any solution. 我尝试了很多选择来删除拥有方的关系,但未找到任何解决方案。 How can i delete the relation on deleting the owning side? 在删除拥有方时如何删除关系?
Added: 添加:
Cascade: 级联:
@ORM\ManyToMany(targetEntity="Neighborhood", inversedBy="projects", cascade={"remove"})
won't help. 不会帮助。 This will delete entity, relation and related entities. 这将删除实体,关系和相关实体。 I need same functionality like $em->remove($neighborhood); 我需要像$ em-> remove($ neighborhood)一样的功能;
I have it, my fault. 我有错,我的错。 I should just update the schema in database (doctrine:schema:update). 我应该只更新数据库中的架构(doctrine:schema:update)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.