我是Symfony2 + Doctrine的新手,我正在寻找一种方法来验证Arraycollection中的唯一性。可能已经回答了问题,但我无法解决它是如何解决的。 我有一个 Relevamientosserviciosprestador 课程,其中包含回调:
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());
}
}
}
Atencionusuarioreclamo 实体:
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;
....
....
....
....
}
我想在给定的 sede 和 relevamiento_id
中挑选 tiporeclamo 的唯一性我使用表单创建或编辑 Relevamientosserviciosprestador ,该表单具有“Atencionusuarioreclamo”实体的子表单集合。提交时, Relevamientosserviciosprestador 的回调会执行,但 $ this-> reclamos 有所有已保存的实体,不仅仅是与 Relevamientosserviciosprestador相关的实体我正在编辑的内容。 这是预期的行为还是我错过了什么? 我还测试了How to validate unique entities in an entity collection in symfony2中提到的方法 但是,它再次检查所有实体。
我也读过Doctrine2 ArrayCollection,但我不明白它是否能解决问题
请问,你能告诉我你如何在持久化之前管理ArrayCollection中的唯一性?
对不起,我的英语很差
提前谢谢你
伊万
答案 0 :(得分:2)
php的array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
与ArrayCollection->toArray()
和ArrayCollection->__construct()
在你的情况下:
$Atencionusuarioreclamo =
new ArrayCollection(array_unique($Atencionusuarioreclamo->toArray());