ZF2 Form / Doctrine 自引用关系下拉列表

ZF2 Form / Doctrine Self Referring Relation Dropdown

首先感谢您对此的调查。 我正在构建一个表单以将类别添加到 db table,这些类别可以有一个父类别(自我引用)。 select 父类别有一个下拉列表。我正在使用 ZF2 和 Doctrine 2 来构建此表单。一切正常,但我唯一的问题是在编辑页面上,父类别下拉列表也显示了当前类别。我想知道如何将其从下拉列表中排除。我在下面发布了一些我的代码。为了简单起见,我删除了一些不相关的行并缩短了一些名称。

我在模型上定义了自引用关系

//Category model
use Doctrine\ORM\Mapping as ORM;
Class Category {
/**
 *
 * @var integer
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;
.....
.....
/**
 * Parent category if available
 * @var self 
 * @ORM\OneToOne(targetEntity="Category")
 * @ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true)
 */
protected $parent;

在表单上,​​我有一个列出所有类别的下拉列表

$parent = new \DoctrineModule\Form\Element\ObjectSelect('parent');
$parent->setOptions(array(
        'label' => 'Parent Category',
        'class' => '',
        'object_manager' => $this->em,
        'target_class' => \Data\Entity\Category::class,
        'property' => 'name',
        'display_empty_item' => true,
        'empty_item_label'   => '- Select Category -',
        'required' => false
    ))->setAttributes(array(
        'class' => 'form-control'
    ));

在编辑控制器上我加载表单并将其绑定到数据库条目

public function editAction()
{
    //get id from url
    $id = $this->params()->fromRoute('id', 0);

    $request = $this->getRequest();

    //load selected category from database
    $category = $this->em->getRepository(\Data\Entity\Category::class)->find($id);

    //create form
    $form = new Form\Category($this->em);

    //bind selected category to form
    $form->bind($category);

    ......
}

谢谢。

您需要将正在编辑的类别的类别 ID 传递给表单,并设置对象选择搜索参数以将 ID 传递给实体存储库。然后,您需要在存储库中创建搜索查询,以排除搜索结果中返回的类别 ID。

您可以使用简单的 setter 将类别 ID 传递给表单。

protected $categoryId;

public function setCategoryId($categoryId)
{
    $this->categoryId = $categoryId;
}

在您的表单中,您需要类似

的内容
$parent->setOptions(array(
            'label' => 'Parent Category',
            'class' => '',
            'object_manager' => $this->em,
            'target_class' => \Data\Entity\Category::class,
            'property' => 'name',
            'is_method' => true,
            'find_method' => array(
                'name' => 'findCategories',
                'params' => array(
                    'searchParams' => array('id' => $this->categoryId),
                ),
            ),
            'display_empty_item' => true,
            'empty_item_label' => '- Select Category -',
            'required' => false
        ))->setAttributes(array(
            'class' => 'form-control'
        ));

并在您的类别存储库中

public function findCategories($searchParams)
    {
        $builder = $this->getEntityManager()->createQueryBuilder();
        $builder->select('c')
                ->from(\Data\Entity\Category::class, 'c')
                ->where('c.id != ?1')
                ->setParameter(1, $searchParams['id'])
                ->orderBy('c.category', 'ASC');

        return $builder->getQuery()->getResult(Query::HYDRATE_OBJECT);
    }

请注意 orderBy 是可选的。

我希望这是有道理的。