多对多自引用Query_builder [英] Many to Many Self Referencing Query_builder

查看:89
本文介绍了多对多自引用Query_builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Status的实体,可以自我引用多对多的关系来定义每个状态下一个可用状态:

I have an entity called Status that has a self referencing many to many relationship to define what each statuses next available status is:

class Status
{
    private $id;

    //...

    /**
     * @ORM\ManyToMany(targetEntity="Status", mappedBy="nextStatuses")
     */
    private $previousStatuses;

    /**
     * @ORM\ManyToMany(targetEntity="Status", inversedBy="previousStatuses")
     * @ORM\JoinTable(name="status_mapping",
     *      joinColumns={@ORM\JoinColumn(name="status_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="next_status_id", referencedColumnName="id")}
     *      )
     */
    private $nextStatuses;

    /**
     * @ORM\OneToMany(targetEntity="OrderStatus", mappedBy="status")
     * @ORM\OrderBy({"createdTime" = "ASC"})
     */
    private $orderStatuses;

    //...    

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->nextStatuses = new \Doctrine\Common\Collections\ArrayCollection();
        $this->previousStatuses = new \Doctrine\Common\Collections\ArrayCollection();
    }

    //...
}

我正在创建一个表单类型,需要添加状态实​​体作为一个字段,并将其选择限制为 $ nextStatuses 问题是EntityType需要一个query_builder,我不能简单说 $ status-> getNextStatuses()

I am creating a form type and need to add the status entity as a field and limit its choices to $nextStatuses the problem is the EntityType requires a query_builder and I cant simply say $status->getNextStatuses()

我目前拥有的(并且有几个变体没有一个工作):

What I currently have (and have had a few variations of it none of which worked):

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //Current status passed as option
        $status = $options['status'];
        $builder->add('status', EntityType::class, array(
            'class' => 'AppBundle\Entity\Status',
            'query_builder' => function (StatusRepository $er) use ($status) {
                return $er->createQueryBuilder('s')
                    ->leftJoin('s.previousStatuses', 'ps')
                    ->where('s.previousStatuses = :status')
                    ->setParameter('status', $status);
            },));

}

以上查询结果出现错误 [语义错误]行0,col 80靠近'previousStatuses':错误:无效的路径表示。 StateFieldPathExpression或SingleValuedAssociationField。

above query results in the error [Semantical Error] line 0, col 80 near 'previousStatuses': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

推荐答案

previousStatuses 关联是一个 ArrayCollection 所以对于Doctrine这个语句是错误的:

the previousStatuses association is a ArrayCollection so for Doctrine this statement is wrong:

->where('s.previousStatuses = :status')

使用 ps 别名其中 clausule,这样:

Use the ps alias in where clausule, something like this:

//...
return $er->createQueryBuilder('s')
   ->leftJoin('s.previousStatuses', 'ps')
   ->where('ps.id = :status') // <---- here
   ->setParameter('status', $status);

我认为状态字段状态实体,但这可能是名称或任何其他,检查您的实体。

I suppose the status field for Status entity, but this could be name or any other, checks this in your entity.

这篇关于多对多自引用Query_builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆