A doutrina manyToMany retorna PersistentCollection em vez de ArrayCollection
Estou trabalhando com o Symfony 3.1 e o Doctrine 2.5.
Eu configuro um relacionamento manyToMany como sempre:
manyToMany:
placeServices:
targetEntity: Acme\MyBundle\Entity\PlaceService
joinTable:
name: place_place_service
joinColumns:
place_id:
referencedColumnName: id
inverseJoinColumns:
place_service_id:
referencedColumnName: id
E adicionar métodos à minha entidade
protected $placeServices;
...
public function __construct()
{
$this->placeServices = new ArrayCollection();
}
...
/**
* @return ArrayCollection
*/
public function getPlaceServices(): ArrayCollection
{
return $this->placeServices;
}
/**
* @param PlaceServiceInterface $placeService
* @return PlaceInterface
*/
public function addPlaceService(PlaceServiceInterface $placeService): PlaceInterface
{
if(!$this->placeServices->contains($placeService)) {
$this->placeServices->add($placeService);
}
return $this;
}
/**
* @param PlaceServiceInterface $placeService
* @return PlaceInterface
*/
public function removePlaceService(PlaceServiceInterface $placeService): PlaceInterface
{
if($this->placeServices->contains($placeService)) {
$this->placeServices->removeElement($placeService);
}
return $this;
}
O problema é que, quando carrego minha entidade, a doutrina coloca umPersistentCollection na propriedade $ this-> placeServices. Isso não parece ser um grande problema, exceto que quando eu construo um formulário para conectar essas duas entidades (várias caixas de seleção simples com o tipo de formulário symfony), quando $ form-> handleRequest () é acionado, o Doctrine tenta injetar os novos dados. na minha entidade e gere um erro se o método get / add / remove não estiver usandoArrayCollection.
Posso forçar meus métodos getter / add / remove a transformar o PersistentCollection em ArrayCollection (usando o método desembrulhar), mas as relações feitas não são mantidas.
Encontrei uma solução alternativa, se eu definirbuscar = "EAGER" na relação, a propriedade é inicializada com ArrayCollection e a relação é mantida. Mas não tenho certeza se é uma boa solução.
Obrigado :)