嘿伙计们,我有接下来的两个实体:
class CarInqueritoExtraFields
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var DbaCustomFields
*
* @ORM\ManyToOne(targetEntity="DbaCustomFields", inversedBy="inqueritoExtraFields")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_custom_field", referencedColumnName="id", onUpdate="cascade", onDelete="cascade", nullable = false)
* })
*/
private $idCustomField;
}
class DbaCustomFields
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="CarInqueritoExtraFields", mappedBy="idCustomField", cascade={"persist"})
*/
private $inqueritoExtraFields;
/**
* @var integer $idContacto
*
* @ORM\Column(name="nome", type="string", nullable=true)
*/
private $nome;
}
这两个实体都有获取/设置。我的 DbaCustomFieldsRepository 中有以下方法:
public function getCustomFieldsNotUsed($idInquerito) {
$qb = $this->getEntityManager()->createQueryBuilder();
$qb2 = $this->getEntityManager()->createQueryBuilder();
$qb->select('cf')
->from('CareAdminBundle:DbaCustomFields', 'cf')
->where($qb->expr()->notIn('cf.id', $qb2->select('ief.idCustomField')
->from('CareAdminBundle:CarInqueritoExtraFields', 'ief')
->where('ief.idInquerito = :idInquerito')
->getDQL()))
->setParameter('idInquerito', $idInquerito);
return $qb;
}
我在我的 formBuilder 中得到了 $qb:
public function buildForm(FormBuilder $builder, array $options){
$id_inquerito = $this->id_inquerito;
$builder->add('idCustomField', 'entity', array(
'empty_value' => 'Escolha um campo',
'class' => 'Care\AdminBundle\Entity\DbaCustomFields',
'required' => false,
'query_builder' => function(\Care\AdminBundle\Repository\DbaCustomFieldsRepository $repository) use ($id_inquerito){
return $repository->getCustomFieldsNotUsed($id_inquerito);
}));
}
但是我收到了这个错误,但我不知道为什么:
在第 19 行的 ::base.html.twig 中呈现模板(“[Semantical Error] line 0, col 80 near 'idCustomField': Error: Invalid PathExpression. Must be a StateFieldPathExpression.”)时引发了异常.
我认为问题应该出在我的 QueryBuilder 中,但我不知道为什么.. 任何帮助表示赞赏!谢谢 :)
----------------------------------更新---------------- ------------------------------ 好吧,我设法得到了解决方案,也许不是正确的方法,但它很好用. 问题出在查询构建器上,所以我这样做了:
//First i pick the CarInqueritoExtraFields By IdInquerito and save the idCustomField from the retrieved object in a array.
$em = $this->getEntityManager();
$carInqueritoExtraFieldsByIdInquerito = $em->getRepository('CareAdminBundle:CarInqueritoExtraFields')->findByIdInquerito($idInquerito);
$arrayExtraFields = array();
foreach ($carInqueritoExtraFieldsByIdInquerito as $extraField) {
array_push($arrayExtraFields, $extraField->getIdCustomField()->getId());
}
//Then this:
$qb->select('cf')
->from('CareAdminBundle:DbaCustomFields', 'cf')
->where($qb->expr()->notIn('cf.id', $arrayExtraFields));
如果有人有更好的方法,请随时告诉我:)