1

我要恢复 InscritSeminaireType 中的课程实体:

class InscritSeminType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder
            //->add('dateInscrit')
            ->add('seminaire','entity', array(
                'class' => 'EspritSeminaireBundle:Seminaire',
                'query_builder' => function($repository)use ($id) {
                    return $repository->getSeminaireLibre('id')
                        ->setParameter('id', $id)
                    ;
                },                          
                'required' => false,            
             ))
        //->add('etudiant')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Esprit\SeminaireBundle\Entity\InscritSemin',

        ));
    }

    public function getName() {
        return 'esprit_seminairebundle_inscritsemintype';
    }
}

但它返回一个错误:

注意:未定义变量:C:\wamp\www\PFE\EOL\src\Esprit\SeminaireBundle\Form\InscritSeminType.php 第 22 行中的 id

那么有人可以告诉我如何解决这个问题吗?

4

1 回答 1

1

如果我正确理解您想在表单构建器中从其 id 恢复精确的“Seminaire”,则必须先将 id 传递给类。

这是有关如何执行此操作的示例。您必须在两个地方进行更改。

首先在控制器类中:

// NOTICE THE $id, you have to pass it to the constructor.
$form = $this->createForm(new InscritSeminType($id), $entity);

然后在表单类型中:

class InscritSeminType extends AbstractType {

    private $id;

    public function __construct($id){
        $this->id = $id // Keep the id of the item.
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $id = $this->id; // Retrieve the id

        $builder
            ->add('seminaire','entity', array(
                'class' => 'EspritSeminaireBundle:Seminaire',
                'property' => 'id', // To tell the mapping is on the id.
                'query_builder' => function($repository) use ($id) {
                    return $repository->getSeminaireLibre($id)
                },      
                'required' => false,
               ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Esprit\SeminaireBundle\Entity\InscritSemin',

        ));
    }

    public function getName() {
        return 'esprit_seminairebundle_inscritsemintype';
    }
}

您在构造函数中传递 id,然后在 buildForm 函数中检索它。

于 2013-04-13T15:36:46.353 回答