4

I need your help today. I'm working on a small application using Symfony 2.1 but I have a base problem, I have to tables with a many to many relation which creates a third table:

class Usuario implements UserInterface {
/**
* @ORM\ManyToMany(targetEntity="Alood\BackBundle\Entity\Alergeno", inversedBy="usuarios")
* @ORM\JoinTable(name="UsuariosProductos",
 *      joinColumns={@ORM\JoinColumn(name="usuario_user", referencedColumnName="user")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="alergeno_id", referencedColumnName="id")}
 *      )
**/
protected $alergenos;
}


public function __construct(){
    $this->alergenos = new ArrayCollection();
}

public function getAlergenos() { return $this->alergenos; }

and:

/**
* @ORM\ManyToMany(targetEntity="Alood\BackBundle\Entity\Usuario", mappedBy="alergenos")
**/
protected $usuarios;

Then I need to remove the non selected Alergenos, this is my controller:

$alergenosUser = $em->getRepository("BackBundle:Usuario")->find($usuario);

$resultSym = array_diff($alergenosUsuarioIds, $alergen);

foreach($resultSym as $result) {
    $alergenosUser->getAlergenos()->remove($result);
}
$em->persist($alergenosUser);
$em->flush();

Could you please help me to figure out what I'm doing wrong? Thanks you so much!

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
soni
  • 687
  • 1
  • 8
  • 23
  • what exactly is the problem with your code? ... despite you're persisting object's already managed by doctrine which is not necessary in this case? :) – Nicolai Fröhlich Aug 30 '13 at 09:06
  • Lets say that I have user 1 and allergen 1 and then I say that user 1 has allergy to 1, I can do that, but when I want to delete that connection (user 1 has no longer allergy to allergen 1) I can't delete that relation. – soni Aug 30 '13 at 09:23

3 Answers3

5

In order to remove an item from a collection use the following:

$collection->removeElement($item);

The remove($key) function will remove by key while removeElement($item) removes the item from the collection if found. Have a look at the ArrayCollection code here.

Be aware that doctrine will only check the owning side of a relation for changes.

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • So, I tried changing $em->persist($alergenosUser); for $em->remove($alergenosUser); but it deletes everything even the user, I just want to remove the data from the third table. Could you help me please? – soni Aug 30 '13 at 09:21
  • so you just want to remove the relation between two entities? ( which would result in removing the table row in the jointable ) – Nicolai Fröhlich Aug 30 '13 at 09:22
  • Exactly! That's what I want :) – soni Aug 30 '13 at 09:24
  • It worked, I tried it before and it didn't worked because I had a orphanRemoval=true in my entity. Thanks! – soni Sep 01 '13 at 07:50
0

It is not clear what the $alergenosUsuarioIds and $alergen variables represent but you might be mistaken about the usage of the remove() method of ArrayCollection. You need to give it an index, not the id of the entity you want to remove. You can also use the removeElement() method and pass it the entity.

For instance you can do something like this :

$elements = $alergenosUser->getAlergenos();
foreach ($elements as $element) {
    if ($element->getId() == $id_from_array_diff_or_whatever) {
        $elements->removeElement($element);
    }
}

or

$elements = $alergenosUser->getAlergenos();
foreach ($elements as $key => $element) {
    if ($element->getId() == $id_from_array_diff_or_whatever) {
        $elements->remove($key);
        // or
        unset($elements[$key]);
    }
}

You can also use the matching() but I'm not sure it's available with the version shipped with symfony2 2.1.

jchampion
  • 381
  • 1
  • 3
0

So your problem can be solved doing the relation yourself. ManyToMany doesn't really exist because as you say a third table is created. You want to delete elements only in this third table.

So you have to build the relation yourself to delete directly an element in the third table.

So first create the third entity. Do two relation ManyToOne from the third entity to the two others entities. Then you just have to simply remove an element of the third entity you just created.

ybert
  • 852
  • 10
  • 18
  • I think this is not a right way to achieve this, I did it using $collection->removeElement($item); like @nifr I just had to delete the orphanRemoval=true of my doctrine entity. But thanks anyway! – soni Sep 01 '13 at 16:13