Symfony2容器意识形式 [英] Symfony2 Container Aware Form Type

查看:100
本文介绍了Symfony2容器意识形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



作为一个例子,我有3个实体帐户,用户和事件。用户具有ManyToMany关系,以便将许多用户与许多其他用户(称为审批者)相关联,因此,用户创建的事件可以包含可以批准该用户的用户列表。在用户编辑表单中,我有一个批准者多个选择字段,列表需要由帐户过滤,因此我需要我的表单类型为容器感知,以便通过帐户ID过滤可用用户列表。



我正在考虑使表单类型容器知道是正确的方法吗?我想使用实体管理器按帐户筛选用户列表。

解决方案

1 通过构造函数注入实体管理器

 <?php 

命名空间Acme\YourBundle\\ \\Form\Type;

使用Symfony\Component\Form\AbstractType;
使用Symfony\Component\Form\FormBuilderInterface;
使用Symfony\Component\OptionsResolver\OptionsResolverInterface;
使用Doctrine\ORM\EntityManager;

class YourType extends AbstractType
{

/ **
*实体管理器
*
* @var EntityManager
* /
private $ entityManager;

/ **
* @param EntityManager
* /
public function __construct(EntityManager $ entityManager)
{
$ this-> ; entityManager = $ entityManager;
}

public function buildForm(FormBuilderInterface $ builder,array $ options)
{
//在这里建立你的表单



}

public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=> ;'Acme\YourBundle\Entity\YourEntity',
));
}

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

}

2 将其声明为服务

 服务:
#表单类型
acme_your_bundle.name_of_your_form.form.type :
class:Acme\YourBundle\Form\Type\YourType
arguments:
entityManager:@ doctrine.orm.entity_manager






注意:



从Symfony开始,请采取以下建议:



仔细查看的代码FOSMessageBundle ,它将给你准确的需要做的任何事情在symfony,从表单模型,形成工厂,创建特殊的服务(如作曲家,授权者等)。学习这个软件包越多,学习symfony就越快,我保证100%。最后,在具体情况下,查看此捆绑包中的FormFactory


Is there a way to make a form type container aware?

As an example I have 3 entities Account, User and Event. Users have ManyToMany relationship in order to associate many users with many other users (called approvers), reason for this is so that an Event created by a User can have a list of Users who area able approve it. In the User edit form where I have an approvers multiple select field the list needs to be filtered by Account so I need my form type to be container aware in order to filter the list of available Users by Account ID.

Am I right in thinking that making the form type container aware is the right way to go? I'd like to use the entity manager to filter a list of Users by Account.

解决方案

1 Inject the entity manager through the constructor

<?php

namespace Acme\YourBundle\Form\Type;

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

class YourType extends AbstractType
{

    /**
     * The entity manager
     *
     * @var EntityManager
     */
    private $entityManager;

    /**
     * @param EntityManager
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       //build your form here



    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\YourBundle\Entity\YourEntity',
        ));
    }

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

}

2 Declare it as a service

services:
    # Form type
    acme_your_bundle.name_of_your_form.form.type:
        class: Acme\YourBundle\Form\Type\YourType
        arguments:
            entityManager: "@doctrine.orm.entity_manager"


Note:

If you're starting with Symfony, take this advice:

look very closely at the code of the FOSMessageBundle, it will give you exactly what you need to do anything in symfony, from form models, to form factories, to the creation of special services (like composer, authorizer, etc..). The more you study this bundle, the quicker you will learn symfony, I guarantee you that 100%. Finally, in your specific case, look at the FormFactory in this bundle

这篇关于Symfony2容器意识形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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