Symfony2:排序/排序翻译的实体表单字段? [英] Symfony2 : Sort / Order a translated entity form field?

查看:195
本文介绍了Symfony2:排序/排序翻译的实体表单字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用的是symfony翻译工具,所以我不能用SQL语句排序值。
有没有在加载和翻译后对值进行排序的方法?

也许使用表单事件?

$ builder
- > add('country','entity',
array(
' class'=>'MyBundle:Country',
'translation_domain'=>'countries',
'property'=>'name',
'empty_value'=>' ---',



解决方案



我找到了在我的表单类型中排序字段值的解决方案。



我们必须使用在创建表单视图时调用的finishView()方法:



 <?php 

命名空间My\Namespace\Form\Type;

使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilderInterface;
使用Symfony \Component\Form\FormView;
使用Symfony \Component\Form\FormInterface;
使用Symfony \ Bundle\FrameworkBundle\Translation\Translator;

class MyFormType extends AbstractType
{
protected $ translator;

public function __construct(Translator $ translator)
{
$ this-> translator = $ translator;

$ b $ public function finishView(FormView $ view,FormInterface $ form,array $ options)
{
//订单翻译国家
$ collat​​or = new \Collat​​or($ this-> translator-> getLocale());
usort(
$ view-> children ['country'] - > vars ['choices'],
function($ a,$ b)use($ collat​​or){
return $ collat​​or-> compare(
$ this-> translator-> trans($ a-> label,array(),'countries'),
$ this-> ; translator-> trans($ b-> label,array(),'countries')
);
}
);
}

// ...

/ **
* @param FormBuilderInterface $ builder
* @param array $ options
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('country','entity',
array(
'class'=>'MyBundle:Country',
'translation_domain'=>'countries',
'property'=>'name',
'empty_value'=>'---',


;
}

}






OLD ANSWER

我发现了一个解决方案,可以在创建视图后在控制器中对它们进行排序: p>



  $ fview = $ form-> createView(); 
usort(
$ fview-> children ['country'] - > vars ['choices'],
function($ a,$ b)use($ translator){
return strcoll($ translator-> trans($ a-> label,array(),'countries'),$ translator-> trans($ b-> label,array(),'countries' ));
}
);

也许我可以以更好的方式做到这一点?
本来我希望直接在我的表单生成器中完成,而不是在使用此表单的控制器中添加额外的代码。


I am trying to order an entity form field witch is translated.

I am using the symfony translation tool, so i can't order values with a SQL statement. Is there a way to sort values after there are loaded and translated ?

Maybe using a form event ?

$builder
    ->add('country', 'entity', 
            array(
                'class' => 'MyBundle:Country',
                'translation_domain' => 'countries',
                'property' => 'name',
                'empty_value' => '---',
            )
        )

解决方案

EDIT

I found the solution to sort my field values in my Form Type.

We have to use the finishView() method which is called when the form view is created :

<?php

namespace My\Namespace\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;

class MyFormType extends AbstractType
{
    protected $translator;

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

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        // Order translated countries
        $collator = new \Collator($this->translator->getLocale());
        usort(
            $view->children['country']->vars['choices'], 
            function ($a, $b) use ($collator) {
                return $collator->compare(
                    $this->translator->trans($a->label, array(), 'countries'), 
                    $this->translator->trans($b->label, array(), 'countries')
                );
            }
        );
    }

    // ...

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('country', 'entity', 
                    array(
                        'class' => 'MyBundle:Country',
                        'translation_domain' => 'countries',
                        'property' => 'name',
                        'empty_value' => '---',
                    )
                )
        ;
    }

}


OLD ANSWER

I found a solution for my problem, I can sort them in my controller after creating the view :

$fview = $form->createView();
usort(
    $fview->children['country']->vars['choices'], 
    function($a, $b) use ($translator){
        return strcoll($translator->trans($a->label, array(), 'countries'), $translator->trans($b->label, array(), 'countries'));
    }
);

Maybe I can do that in a better way ? Originally I wished to do directly in my form builder instead of adding extra code in controllers where I use this form.

这篇关于Symfony2:排序/排序翻译的实体表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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