Symfony2 inserts EntityMananager into FormType

Custom Form Type   

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityManager;

class NationaliteitidType extends AbstractType 
{
private $doctrine;
private $em;

public function __construct(EntityManager  $em)
{
    $this->em = $em;
}

service.yml services:

fw_core.form.type:
    class: FW\CoreBundle\Form\Type\NationaliteitidType
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"

Error:

Argument 1 passed to FW \ CoreBundle \ Form \ TypeNationaliteitidType :: __ construct () must be an instance of Doctrine \ ORM \ EntityManager, not specified,

I must have done a type or something else obvious, but really can't find it.

0
source share
4 answers

In your services.yml you cannot name your future variables, so try something like this:

services :
    fw_core.form.type:
        class: FW\CoreBundle\Form\Type\NationaliteitidType
        arguments: 
            - "@doctrine.orm.entity_manager"
+2
source

I think (it hasn’t worked very well with Service containers so far, so please tolerate me) that you should do the following (already suggested by @Touki):

services.yml:

fw_core.form.type:
    class: FW\CoreBundle\Form\Type\NationaliteitidType
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"

And in your class:

public function __construct($entityManager)
{
    $this->em = $entityManager;
}

, __construct ( entityManager) , services.yml.

: - , . : http://symfony.com/doc/current/book/service_container.html

0

Why do you need to enter an object manager? If you want to fill in a field based on some information obtained from db, you should just do something like

$qb_function = function(EntityRepository $rep) use ($bar_parameter) {
    return $rep->createQueryBuilder('s')
               ->where('s.bar = :bar')
               ->setParameter('bar',$bar_parameter);};
}
$builder->add('foo','entity',array('query_builder')=>qb_function);
0
source

In the controller, use PrintelBundle \ Form \ XXXType;

class XXXController extends Controller
{
....
    public function newAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = new XXX();
        $form = $this->createForm(new XXXType($em), $entity);

....

And in the form of type

class XXXType extends AbstractType
{
    private $em;

    public function __construct($em)
    {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
         $em = $this->em;

........ done

0
source

All Articles