Php 在Symfony2表单中使用选项的函数返回值

Php 在Symfony2表单中使用选项的函数返回值,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我有这个Symfony2表格: public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add( 'nombre', 'text', array( 'required' => true, 'mapped' =

我有这个Symfony2表格:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'nombre',
            'text',
            array(
                'required' => true,
                'mapped'   => false,
                'label'    => 'Nombre'
            )
        )
        ->add(
            'anno_modelo',
            'choice',
            array(
                'placeholder'   => 'Escoja una opción',
                'choices'       => array(),
                'required'      => true
            )
        )
        ->add(
            'anno_fabricacion',
            'choice',
            array(
                'placeholder'   => 'Escoja una opción',
                'choices'       => array(),
                'required'      => true
            )
        );
}
我有这个密码:

function getChoices() {
    $choices = [];
    for($i = date("Y"); $ >= 1900; $i--) {
        $choices[$i - 1] = $i - 1;
    }

    return $choices;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $choices = $this->getChoices();
    $builder
        ->add(
            'nombre',
            'text',
            array(
                'required' => true,
                'mapped'   => false,
                'label'    => 'Nombre'
            )
        )
        ->add(
            'anno_modelo',
            'choice',
            array(
                'placeholder'   => 'Escoja una opción',
                'choices'       => $choices,
                'required'      => true
            )
        )
        ->add(
            'anno_fabricacion',
            'choice',
            array(
                'placeholder'   => 'Escoja una opción',
                'choices'       => $choices,
                'required'      => true
            )
        );
}

private function getChoices() 
{
    $choices = [];
    for($i = date('Y'); $i >= 1900; $i--) {
        $choices[$i - 1] = $i - 1;
    }

    return $choices;
}
我需要将该
$choices
用作
choices
值,我如何实现

请尝试以下代码:

function getChoices() {
    $choices = [];
    for($i = date("Y"); $ >= 1900; $i--) {
        $choices[$i - 1] = $i - 1;
    }

    return $choices;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $choices = $this->getChoices();
    $builder
        ->add(
            'nombre',
            'text',
            array(
                'required' => true,
                'mapped'   => false,
                'label'    => 'Nombre'
            )
        )
        ->add(
            'anno_modelo',
            'choice',
            array(
                'placeholder'   => 'Escoja una opción',
                'choices'       => $choices,
                'required'      => true
            )
        )
        ->add(
            'anno_fabricacion',
            'choice',
            array(
                'placeholder'   => 'Escoja una opción',
                'choices'       => $choices,
                'required'      => true
            )
        );
}

private function getChoices() 
{
    $choices = [];
    for($i = date('Y'); $i >= 1900; $i--) {
        $choices[$i - 1] = $i - 1;
    }

    return $choices;
}

一个旁注,但更简单的创建方法是使用范围函数,如
$range=range(日期('Y'),1900)
获取年份范围,然后使用与使用array\u combine相同的键返回它,如
return array\u combine($range,$range)
@Qoop一个有效的示例,谢谢,这只是一个示例函数,因为我正在处理的函数有点复杂,我还没有完成