Symfony Forms (as a standalone component with Doctrine) EntityType not working

I am using Symfony forms (v3.0) without the rest of the Symfony framework. Using Doctrine v2.5.

I created a form, here is a form type class:

class CreateMyEntityForm extends BaseFormType {

    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('myEntity', EntityType::class);
    }
}

When loading the page, the following error appears.

Argument 1 went to Symfony \ Bridge \ Doctrine \ Form \ Type \ DoctrineType :: __ construct () there must be an instance of Doctrine \ Common \ Persistence \ ManagerRegistry, none given, is called in / var / www / dev 3 / Vendor / symfony / form /FormRegistry.php on line 85

I believe that there is some kind of configuration here, but I do not know how to create a class that implements ManagerRegistryInterface - if that is correct.

Any pointers?

Edit - here is my code for configuring Doctrine

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;

class Bootstrap {

    //...some other methods, including getCredentials() which returns DB credentials for Doctrine

    public function getEntityManager($env){

        $isDevMode = $env == 'dev';

        $paths = [ROOT_DIR . '/src'];

        $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);

        $dbParams = $this->getCredentials($env);

        $em = EntityManager::create($dbParams, $config);

        return $em;
    }
}
+6
3

, !

EntityType::class , "Symfony" ( - DoctrineBundle). .
!

, , ChoiceType::class. - :

<?php
# you form class
namespace Application\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class InvoiceItemtType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('product', ChoiceType::class, [
            'choices' => $this->loadProducts($options['products'])
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(['products' => [],]); # custom form option
    }

    private function loadProducts($productsCollection)
    {
        # custom logic here (if any)
    }
}

- :

$repo = $entityManager->getRepository(Product::class);
$formOptions = ['products' => $repo->findAll()];
$formFactory = Forms::createFormFactory();
$formFactory->create(InvoiceItemtType::class, new InvoiceItem, $formOptions);

, !

+4

xabbuh.

EntityType FormBuilder . , Constraints , .

ManagerRegistry Doctrine ORM, AbstractManagerRegistry ManagerRegistry.

, (ValidatorExtension, HttpFoundationExtension ..).

use \Doctrine\Common\Persistence\AbstractManagerRegistry;

class ManagerRegistry extends AbstractManagerRegistry
{

    /**
     * @var array
     */
    protected $container = [];

    public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName)
    {
        $this->container = $managers;
        parent::__construct($name, $connections, array_keys($managers), $defaultConnection, $defaultManager, $proxyInterfaceName);
    }

    protected function getService($name)
    {   
        return $this->container[$name];
       //alternatively supply the entity manager here instead
    }

    protected function resetService($name)
    {
        //unset($this->container[$name]);
        return; //don't want to lose the manager
    }


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

}

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

class UserType extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       $builder->add('field_name', EntityType::class, [
           'class' => YourEntity::class,
           'choice_label' => 'id'
       ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
       $resolver->setDefaults(['data_class' => YourAssociatedEntity::class]);
    }
}

$managerRegistry = new \ManagerRegistry('default', [], ['default' => $entityManager], null, 'default', 'Doctrine\\ORM\\Proxy\\Proxy');

$extension = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($managerRegistry);

$formBuilder = \Symfony\Component\Form\FormFactoryBuilder::createFormFactoryBuilder();
$formBuilder->addExtension($extension);

$formFactory = $formBuilder->getFormFactory();

$form = $formFactory->create(new \UserType, $data, $options);

! , . [DTO ( ).

2. 5+ ""

class CustomerDTO
{
    public function __construct($name, $email, $city, $value = null)
    {
        // Bind values to the object properties.
    }
}
$query = $em->createQuery('SELECT NEW CustomerDTO(c.name, e.email, a.city) FROM Customer c JOIN c.email e JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
+3

The easiest way to solve your problem is to register DoctrineOrmExtension from the Doctrine bridge, which ensures that the object type is registered using the necessary dependencies.

Thus, the process of loading the Form component will look like this:

// a Doctrine ManagerRegistry instance (you will probably already build this somewhere else)
$managerRegistry = ...;

$doctrineOrmExtension = new DoctrineOrmExtension($managerRegistry);

// the list of form extensions
$extensions = array();

// register other extensions
// ...

// add the DoctrineOrmExtension
$extensions[] = $doctrineOrmExtension;

// a ResolvedFormTypeFactoryInterface instance
$resolvedTypeFactory = ...;

$formRegistry = new FormRegistry($extensions, $resolvedTypeFactory);
+1
source

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


All Articles