Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2.1 - The option "em" does not exist when using DataTransformer

I am using this cookbook recipe to add a data transformer in Symfon 2.1, but I am getting the following error, The option "em" does not exist. Known options are: "attr", "block_name",....

Is this still a valid way to send the entity manager over to the form type?

$taskForm = $this->createForm(new TaskType(), $task, array(
    'em' => $this->getDoctrine()->getEntityManager(),
));
like image 611
Mike Avatar asked Dec 07 '22 13:12

Mike


1 Answers

To make the first simple (without dependency injection) Transformer's cookbook recipe work you should add "em" as a known option. You can add it in your form's type class (TaskType in cookbook case) using setRequired() method like this:

class TaskType extends AbstractType {
    //...
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {   
        //...other stuff like $resolver->setDefaults(... if you need it

        $resolver->setRequired(array('em'));
    }
}

Adding 'em' with $resolver->setDefaults() would also work, but in this cookbook case entity manager is needed and so using setRequired() seems better.

like image 160
mimazoo Avatar answered Feb 17 '23 08:02

mimazoo