Silex + Doctrine2 ORM + Dropdown (EntityType)

I have a controller that displays a form that is supposed to have a drop-down list with headers mapped to the client_user object. Below is the code that I use in my controller to create the form:

$builder = $this->get(form.factory);
$em = $this->get('doctrine.entity_manager');

$form = $builder->createBuilder(new ClientUserType($em), new ClientUser())->getForm();

Below is my ClientUserType class with a constructor that I pass to the entity manager:

<?php

  namespace Application\Form\Type;

  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;

 class ClientUserType extends AbstractType
 { 
   protected $entityManager;

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

   public function buildForm(FormBuilderInterface $builder, array $options)
   {
     $builder
       ->add('title', EntityType::class, array(
         'class' => '\\Application\\Model\\Entity\\Title',
         'em' => $this->entityManager
       ))
      ->add('name')
      ->add('surname')
      ->add('contact')
      ->add('email');
    }

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

I keep getting this fascinating fatal error below and don’t know what I need to do to get a drop-down list with headers from the doctrine database.

: 1 Symfony\Bridge\Doctrine\Form\Type\DoctrineType:: __ construct() Doctrine\Common\Persistence\ManagerRegistry, , D:\web\ \vendor\symfony\form\FormRegistry.php 90 D:\web\playground-solutions\vendor\symfony\doctrine-bridge\Form\Type\DoctrineType.php 111

. , DirectorRegistry, , , . , , , ManagerRegistry .

- ? ?

+4
3

, - .

namespace Your\Namespace;

use Doctrine\Common\Persistence\AbstractManagerRegistry;
use Silex\Application;

class ManagerRegistry extends AbstractManagerRegistry
{
    protected $container;

    protected function getService($name)
    {
        return $this->container[$name];
    }

    protected function resetService($name)
    {
        unset($this->container[$name]);
    }

    public function getAliasNamespace($alias)
    {
        throw new \BadMethodCallException('Namespace aliases not supported.');
    }

    public function setContainer(Application $container)
    {
        $this->container = $container;
    }
}

-

$application->register(new Silex\Provider\FormServiceProvider(), []);

$application->extend('form.extensions', function($extensions, $application) {
    if (isset($application['form.doctrine.bridge.included'])) return $extensions;
    $application['form.doctrine.bridge.included'] = 1;

    $mr = new Your\Namespace\ManagerRegistry(
        null, array(), array('em'), null, null, '\\Doctrine\\ORM\\Proxy\\Proxy'
    );
    $mr->setContainer($application);
    $extensions[] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($mr);

    return $extensions;
});

array('em') - em $application

+3

, : EntityType , DoctrineOrmExtension FormFactoryBuilder :

$managerRegistry = new myManagerRegistry(
    'myManager',
    array('connection'),
    array('em'),
    'connection',
    'em',
    \Doctrine\ORM\Proxy\Proxy::class
);

// Setup your Manager Registry or whatever...

$doctrineOrmExtension = new DoctrineOrmExtension($managerRegistry);
$builder->addExtension($doctrineOrmExtension);

EntityType, myManagerRegistry#getService($name). $name - ("em" "connection"), Doctrine Doctrine .

+1

:

$em = $this->get('doctrine.orm.entity_manager');

, .

Edit:

, , Symfony... ...

0
source

Source: https://habr.com/ru/post/1623801/


All Articles