Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

symfony - How to access the entity inside the buildForm method of a form

I'm trying to get the entity inside the own form. I lazy remember a getData() method, but it doesn't exist inside the form, and I can't remember how to use it.

Is there any way to get the entity inside the buildform method not using an event listener?

I mean, something like this:

 public function buildForm(FormBuilderInterface $builder, array $options)
   {
        /*some method to get the entity of the form such as getEntity????????*/
        $builder->add('field');
   }
question from:https://stackoverflow.com/questions/17345661/how-to-access-the-entity-inside-the-buildform-method-of-a-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found it:

public function buildForm(FormBuilderInterface $builder, array $options)
   {
        $entity = $builder->getData();
        $builder->add('field');
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...