问题
I need your help today. I'm working on a small application using symphony 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!
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/18528951/symfony2-remove-and-save-many-to-many-relations