Doctrine查询构建器错误

时间:2012-06-26 17:11:39

标签: symfony doctrine-orm query-builder

嘿伙计们我有接下来的两个实体:

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);
                    }));     
    }

但我得到了这个错误,但我无法弄清楚原因:

  

在渲染模板期间抛出异常(“[语义错误]第0行,第80行'idCustomField'附近:错误:   无效的PathExpression。必须是StateFieldPathExpression。“)in   :: base.html.twig第19行。

我认为问题应该在我的QueryBuilder中,但我不知道为什么.. 任何帮助表示赞赏! 谢谢:)

----------------------------------的更新 ----- ---------------------------------------- 我设法得到解决方案,也许不是正确的方法,但它确实有效。 问题出在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));

如果有人有更好的方法,请随时告诉我:)

1 个答案:

答案 0 :(得分:2)

你正在尝试嵌套查询构建器,这是完全无效的Doctrine。改为使用连接。