从SonataClassificationBundle获取类别,作为SonataBlockBundle块中的下拉列表

时间:2015-03-17 15:06:45

标签: php symfony sonata-admin

我正试着在自定义SonataClassificationBundle区块中SonataBlockBundle中的所有类别下拉。

现在我正在我的块类中自己构建类别树。这是不正确的。但我不知道如何配置我的表单以使其自动运行。

我现在使用的代码:

    $categories = $this->categoryManager->getCategories('default');

    if (!function_exists('buildCategoryTreeRecursive'))
    {
        function buildCategoryTreeRecursive ($categories, $buffer = null, $level = 0)
        {
            if ($buffer == null)
            {
                $buffer = array();
            }

            foreach ($categories as $category)
            {
                $buffer[$category->getId()] = str_repeat('-', $level).($level > 0 ? ' ' : '').$category->getName();

                $children = $category->getChildren();

                $buffer = buildCategoryTreeRecursive ($children, $buffer, $level + 1);
            }

            return $buffer;
        }
    }

    $categoryTree = buildCategoryTreeRecursive($categories);

    $formMapper->add('settings', 'sonata_type_immutable_array', array(
        'keys' => array(
            array('title',    'text',    array('required' => true)),
            array('subtitle', 'text',    array('required' => false)),
            array('category',  'choice', array('required' => true, 'choices' => $categoryTree)),
        )
    ));
}

我期望的代码:

    $formMapper->add('settings', 'sonata_type_immutable_array', array(
        'keys' => array(
            array('title',    'text',    array('required' => true)),
            array('subtitle', 'text',    array('required' => false)),
            array('category'),
        )
    ));

类别的定义如下:

/**
 * @ORM\ManyToOne(targetEntity="Application\Sonata\ClassificationBundle\Entity\Category")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;

当我在Sonata\AdminBundle\Admin\Admin子类中使用它时,这会产生(下拉列表生成):

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->with('content')
        ->add('category')
        ->end()
    ;
}

0 个答案:

没有答案