Manium
Manium

Reputation: 65

Doctrine remove owning side Many To Many relation

I have this relation between 2 Entities in Symfony 3:

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); delete the neighborhood in the table 'neighborhood' and all relations in the table 'neighborhood_project'.

$em->remove($project); delete the project in the table 'project' and DOES NOT delete the relation in the table 'neighborhood_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);

Upvotes: 0

Views: 812

Answers (1)

Manium
Manium

Reputation: 65

I have it, my fault. I should just update the schema in database (doctrine:schema:update).

Upvotes: 0

Related Questions