Trouble with using entity Field Type field with choice in case of standalone symfony-form component

245 views Asked by At

I use symfony-form component as standalone.

Here is composer.json :

    "symfony/form": "2.7.*",
    "symfony/validator": "2.7.*",
    "symfony/config": "2.7.*",
    "symfony/templating": "2.8.*@dev",
    "symfony/twig-bridge": "3.0.*@dev",
    "symfony/framework-bundle": "2.7.0-BETA1",
    "symfony/doctrine-bridge":"2.7.1"

I want to create choice form element with entity type. But how ? When i set string parameter, it says "Could not load type "entity".

 $builder
        ->add('groups', 'entity', array(
        'class' => 'Application\Groups\Entity\Group',
        'choices' => $this->group->getUsers()
    ));

How to load entityType in form? Here is my form initialization:

  /**
 * @return Component\Form\FormFactoryInterface
 */
public function getFormFactory()
{
    if(!$this->formFactory){
        $this->formFactory = Component\Form\Forms::createFormFactoryBuilder()
            ->addExtension(new Extension\HttpFoundation\HttpFoundationExtension())
            ->addExtension(new Extension\Templating\TemplatingExtension($this->engine->getEngine(), null, array(
                $VENDOR_FRAMEWORK_BUNDLE_DIR . '/Resources/views/Form',
                PATH_APPLICATION.'/layouts/form_widgets'
            )))
            //->addExtension(new CsrfExtension($this->initCSRF()))
            ->addExtension(new Extension\Validator\ValidatorExtension($this->initValidator()))
            ->getFormFactory();
    }

    return $this->formFactory;
}

Maybe i should to set object new Symfony\Bridge\Doctrine\Form\Type\EntityType(). But it needs ManagerRegistry which i have not. I really don't need ManagerRegistry because i have only one EntityManager which manage all my entities by own. Perhabs i should to make some fake object-wrapper which implements ManagerRegistry interface and inject it to new EntityType()

Please help me with this huge trouble.

1

There are 1 answers

0
Kalmár Gábor On

A little late, but here is a very ugly way to get you started:

The fake ManagerRegistry initialisation:

$doctrineManagerRegistry = new TestManagerRegistry(
            'ORM',
            array('default_connection'),
            array('default_manager'),
            'default',
            'default',
            'Doctrine\Common\Persistence\ObjectManagerAware'
        );

This goes to formFactory:

->addExtension(new DoctrineOrmExtension($doctrineManagerRegistry))

And the fake ManagerRegistry:

class TestManagerRegistry extends \Doctrine\Common\Persistence\AbstractManagerRegistry
{
    protected function getService($name)
    {
        return App::$em; //your entity manager
    }

    protected function resetService($name)
    {

    }

    public function getAliasNamespace($alias)
    {
        return __NAMESPACE__;
    }
}

These codes are not for production, just for ideas, they are mostly from: vendor/doctrine/common/tests/Doctrine/Tests/Common/Persistence/ManagerRegistryTest.php

I will update this answer, if I come up with a good way of doing this.