I'm new to Symfony2 + Doctrine and I´m looking for a way to validate the uniqueness in an Arraycollection. May be it is already answered question but I can´t figure how resolve it. I`ve a Relevamientosserviciosprestador class with a Callback:
namespace Prestadores\PrincipalBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\ExecutionContext;
/**
* Prestadores\PrincipalBundle\Entity\Relevamientosserviciosprestador
*
* @ORM\Table(name="relevServiciosPrestador")
* @ORM\Entity(repositoryClass="Prestadores\PrincipalBundle\Repository\RelevamientosserviciosprestadorRepository")*
* @Assert\Callback(methods={"sonUnicosLosTiposDeReclamoq"})
*/
class Relevamientosserviciosprestador
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
....
....
/**
* @ORM\OneToMany(targetEntity="Atencionusuarioreclamo", mappedBy="relevamiento_id", cascade={"persist"})
* @Assert\Valid
*/
private $reclamos;
....
....
public function __construct()
{
$this->personal = new ArrayCollection();
$this->reclamos = new ArrayCollection();
}
....
....
/*Acá intentaremos validar si los tipos de reclamo que se están cargando son únicos para ese relevamiento*/
public function sonUnicosLosTiposDeReclamoq(ExecutionContext $context)
{
foreach ($this->reclamos as $reclamo){
/*Here, I get all entities, not only those related to a Relevamientosserviciosprestador*/
var_dump($reclamo->gettiporeclamo()->getnombre());
}
}
}
And the Atencionusuarioreclamo entity:
namespace Prestadores\PrincipalBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Prestadores\PrincipalBundle\Entity\Atencionusuarioreclamo
*
* @ORM\Table(name="atencionUsuarioReclamo")
* @ORM\Entity
*/
class Atencionusuarioreclamo
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var Atencionusuariosede
*
* @ORM\ManyToOne(targetEntity="Atencionusuariosede")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="nroSede", referencedColumnName="id")
* })
*/
private $nrosede;
/**
* @var relevamiento_id
*
* @ORM\ManyToOne(targetEntity="Relevamientosserviciosprestador")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="relevamiento_id", referencedColumnName="id")
* })
*/
private $relevamiento_id;
/**
* @var Prmreclamotipo
*
* @ORM\ManyToOne(targetEntity="Prmreclamotipo")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="tipoReclamo", referencedColumnName="id")
* })
* @Assert\NotBlank()
*/
private $tiporeclamo;
....
....
....
....
}
I want to chek uniqueness of tiporeclamo in a given sede and relevamiento_id
I create or edit a Relevamientosserviciosprestador using a Form which has a sub form collection for "Atencionusuarioreclamo" entities. On submit, the callback for Relevamientosserviciosprestador executes but $this->reclamos has all saved entities not only those related to the Relevamientosserviciosprestador what I´m editing. Is this the expected behauvoir or I`m missing something? I´ve also tested the approach mentioned in How to validate unique entities in an entity collection in symfony2 but, again, it checks all entities.
I´ve also read Doctrine2 ArrayCollection but I cant´t understand if it resolve the problem.
Please, can you tell me how do you manage uniqueness in your ArrayCollection before persist it?
I´m sorry for my poor english
Thanks in advance
Ivan
php's
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
combined with
ArrayCollection->toArray()
andArrayCollection->__construct()
In your case: