The DON’Ts of Symfony 2 DI on Abstract Classes

This is in response to the use of DI on Abstract Classes. The discussion originated here but I think it is a matter of importance so i am turning this into a blog post.

hi Bernhard, I don’t think he has listened you yet, judging for his latest post on collection forms he still passes does it twice:

Here:

$form = $this->createForm(new \Khepin\GolfBundle\Form\ParSetType($course));
 
// should be
$form = $this->createForm(new \Khepin\GolfBundle\Form\ParSetType(), $course);

And here:

 $builder->get('holes')->add('hole_'.$i, new HoleType($hole));
 
// should be
 $builder->get('holes')->add('hole_'.$i, new HoleType(), $hole); // <--- Is this possible!!! I wonder about the add signature!!

So I found out there is a space for a data option here:
So the code should now look like:

$builder->get('holes')->add('hole_'.$i, new HoleType(), array( 'data' => $hole ) );

I have not tried if this works but at least it has some support.

@Bernhard can you please help me out with the last part? how it should be done?

thanks! I will update this blog post as this is resolved.

2 thoughts on “The DON’Ts of Symfony 2 DI on Abstract Classes

    • thanks Bernhard, i think it was helpful. I resolved my issue another way, it is related but not really this. Thanks anyway. Whatever helps us understand the form component better works.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>