I have an entity Room that has a ManyToMany relationship with itself, and this relationship (RoomLinkRoom) bears an attribute "weight" : a Room can be linked to several Rooms, with an ordering(weight) value (and potentially other attributes).
The code i have sofar handles the linkage of one or more RoomTo to a RoomFrom. Let's say we have : RoomA : 1 RoomB : 2
In the Room Form, we can add RoomB to RoomA we have this entry in RoomLinkRoom :
RoomFromId : 1 RoomToId : 2
But this is only half of what i need : i also need the reverse part management.
When RoomFromId,1 is linked to RoomToId,2 i also want to add in this intermediate entity : RoomFromId,2, RoomToId,1
And then i also have to manage the delete part : If i remove Room A, the entry (roomFrom, roomTo) : (1, 2) will be deleted but the reverse (2, 1) must also be deleted.
How can i achieve this ? What is the best (cleanest ?) way to handle this whole problem ? Is there a "standard pattern" ("automatic" or not) for handling this case ? I'm not sure how to procede, but maybe it involves events, like postFlush ? But will it be able to take care also of the delete for the "reverse" (To) side ?
The (relevant part of the) Entities are :
<?php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use MyBundle\Entity\RoomLinkRoom;
/**
* Room
*
* @ORM\Table(name="room")
* @ORM\Entity(repositoryClass="MyBundle\Repository\RoomRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Room
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* @ORM\OneToMany(
* targetEntity="MyBundle\Entity\RoomLinkRoom", mappedBy="roomFrom",
* cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $roomFromLinks;
/**
* @ORM\OneToMany(
* targetEntity="MyBundle\Entity\RoomLinkRoom", mappedBy="roomTo",
* cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $roomToLinks;
public function __construct()
{
$this->roomFromLinks = new ArrayCollection();
$this->roomToLinks = new ArrayCollection();
}
public function addRoomFromLink(RoomLinkRoom $roomFromLink)
{
$this->roomFromLinks[] = $roomFromLink;
$roomFromLink->setRoomFrom($this);
return $this;
}
public function removeRoomFromLink(RoomLinkRoom $roomFromLink)
{
$this->roomFromLinks->removeElement($roomFromLink);
}
public function getRoomFromLinks()
{
return $this->roomFromLinks;
}
public function addRoomToLink(RoomLinkRoom $roomToLink)
{
$this->roomToLinks[] = $roomToLink;
$roomToLink->setRoomTo($this);
return $this;
}
public function removeRoomToLink(RoomLinkRoom $roomToLink)
{
$this->roomToLinks->removeElement($roomToLink);
}
public function getRoomToLinks()
{
return $this->roomToLinks;
}
}
And
<?php
use Doctrine\ORM\Mapping as ORM;
use MyBundle\Entity\Room;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* RoomLinkRoom.
*
* @ORM\Table(name="roomlinkroom", indexes={
* @ORM\Index(name="FK_RoomLinkRoom_roomfromId", columns={"roomfromId"}),
* @ORM\Index(name="FK_RoomLinkRoom_roomtoId", columns={"roomtoId"}),
* })
* @ORM\Entity(repositoryClass="MyBundle\Repository\RoomLinkRoomRepository")
* @UniqueEntity(
* fields={"roomFrom", "roomTo"},
* errorPath="weight",
* message="Ces salles sont déjà liées."
* )
*/
class RoomLinkRoom
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\Room", inversedBy="roomFromLinks")
* @ORM\JoinColumn(name="roomFromId", referencedColumnName="id", nullable=false)
*/
private $roomFrom;
/**
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\Room", inversedBy="roomToLinks")
* @ORM\JoinColumn(name="roomToId", referencedColumnName="id", nullable=false)
*/
private $roomTo;
public function getId()
{
return $this->id;
}
public function setRoomFrom(Room $roomFrom)
{
$this->roomFrom = $roomFrom;
return $this;
}
public function getRoomFrom()
{
return $this->roomFrom;
}
public function setRoomTo(Room $roomTo)
{
$this->roomTo = $roomTo;
return $this;
}
public function getRoomTo()
{
return $this->roomTo;
}
}
I've tried in the controller to add after the flush :
if ($form->isSubmitted() && $form->isValid()) {
//...
$em->persist($room);
$em->flush();
//look for existing relationship between To and From
foreach ($room->getRoomFromLinks() as $rfl){
$res = $em->getRepository('MyBundle:RoomLinkRoom')
->findBy(['roomFrom' => $rfl->getRoomTo(), 'roomTo' => $room]);
//add the reverse side
if (count($res) === 0){
$rlr = new RoomLinkRoom();
$rlr->setRoomFrom($rfl->getRoomTo());
$rlr->setRoomTo($room);
$em->persist($rlr);
$em->flush();
}
}
//redirect
}
This adds the reverse/complementary entry, but i still don't know if this is clean/bugproof. But i'm sure this doesn't take care of the delete.
So does anyone have used this kind of relationship ?
I've seen other discussions here (for instance, this one) but, they don't seem to care for this "reverse" management.
Thank you in advance.