如何使silex中的实体字段类型可用? [英] How can I make the entity field type available in silex?

查看:73
本文介绍了如何使silex中的实体字段类型可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Silex作为我最新的项目,而且我试图跟随如何动态修改表单使用表单事件在Symfony食谱。我得到了使用实体字段类型的部分,并意识到它在Silex中不可用。

I have been using Silex for my latest project and I was trying to follow along with the "How to Dynamically Modify Forms Using Form Events" in the Symfony cookbook. I got to the part that uses the entity field type and realized it is not available in Silex.

看起来像symfony / doctrine-bridge可以添加到包含EntityType的composer.json中。有没有人成功获得实体类型在Silex中工作或遇到这个问题,找到一个解决方法?

It looks like the symfony/doctrine-bridge can be added to my composer.json which contains the "EntityType". Has anyone successfully got entity type to work in Silex or run into this issue and found a workaround?

我在想这样可能会起作用:

I was thinking something like this might work:

    $builder
        ->add('myentity', new EntityType($objectManager, $queryBuilder, 'Path\To\Entity'), array(
    ))
    ;

我还发现这个答案看起来可能会通过扩展form.factory来做这个伎俩,但还没有尝试过。

I also found this answer which looks like it might do the trick by extending the form.factory but haven't attempted yet.

推荐答案

我使用这个 Gist在Silex中添加EntityType字段。

I use this Gist to add EntityType field in Silex.

但是,诀窍是通过扩展 form.extensions 来注册 DoctrineOrmExtension 表单扩展名 FormServiceProvider doc说。

But the trick is register the DoctrineOrmExtension form extension by extending form.extensions like FormServiceProvider doc says.

DoctrineOrmExtension 在其构造函数中期望一个 ManagerRegistry 接口,可以实现扩展 Doctrine\\ \\Common\Persistence\AbstractManagerRegistry 如下:

DoctrineOrmExtension expects an ManagerRegistry interface in its constructor, that can be implemented extending Doctrine\Common\Persistence\AbstractManagerRegistry as the follow:

<?php
namespace MyNamespace\Form\Extensions\Doctrine\Bridge;

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

/**
 * References Doctrine connections and entity/document managers.
 *
 * @author Саша Стаменковић <umpirsky@gmail.com>
 */
class ManagerRegistry extends AbstractManagerRegistry
{

    /**
     * @var Application
     */
    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['orm.ems'];

    }

}

所以,注册我使用的表单扩展名:

So, to register the form extension i use:

// Doctrine Brigde for form extension
$app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions) use ($app) {
    $manager = new MyNamespace\Form\Extensions\Doctrine\Bridge\ManagerRegistry(
        null, array(), array('default'), null, null, '\Doctrine\ORM\Proxy\Proxy'
    );
    $manager->setContainer($app);
    $extensions[] = new Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($manager);

    return $extensions;
}));

这篇关于如何使silex中的实体字段类型可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆