php - Removing OneToMany elements, Doctrine2 -


i've got model; itinerary, venue, itineraryvenue.

i needed many many relation between itineraries , venues wanted store specific data relation (say notes, own photo, etc.), decided introduce new entity named itineraryvenue.

so itinerary has collection of itineraryvenues in turn, refer venues.

my problem can't remove itineraryvenue itinerary object.

$itinerary->itineraryvenues->removeelement($itineraryvenue); $em->flush(); 

removes element php collection, doesn't remove $itineraryvenue database.

i've managed force doctrine2 remove $itineraryvenue, when annotate itinerary::$itineraryvenues orphanremoval=true.

since orphan removal treats venue private property removes venue entity, don't want that.

is there relation configuration option or removing "by hand" olny way make work want?

hard believe it, it's common relation pattern.

entities definitions:

class itinerary {     /**      * @orm\onetomany(targetentity="itineraryvenue", mappedby="itinerary", cascade={"persist", "remove"})      */     private $itineraryvenues;      function __construct()     {         $this->itineraryvenues = new arraycollection();     } }  class itineraryvenue {     /**      * @orm\manytoone(targetentity="itinerary", inversedby="itineraryvenues")      */     private $itinerary;     /**      * @orm\manytoone(targetentity="venue")      */     private $venue;      function __construct()     {     } }  class venue { } 

you doing things right: orphanremoval - need. so, should override default itinerary::removeitineraryvenue

public function removeitineraryvenue(\appbundle\entity\itineraryvenue $itineraryvenue) {     $itineraryvenue->setitinerary(null);     $this->itineraryvenues->removeelement($itineraryvenue); } 

the full working example here https://github.com/kaduev13/removing-onetomany-elements-doctrine2.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -