ZF2形式/学说自指关系下拉列表


ZF2 Form / Doctrine Self Referring Relation Dropdown

首先感谢您对此进行调查。我正在构建一个表单,将类别添加到数据库表中,这些类别可以有一个父类别(自引用)。有一个下拉列表来选择父类别。我使用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是可选的。

我希望这是有道理的。