链接到实体的Data Transformer选择类型

时间:2014-04-09 14:45:48

标签: forms symfony-2.4

我希望你能理解得很好......

我有一个实体:'模型',其中包含attribut'ediblelangs'格式,如:,es_ES,fr_FR,

我有一个实体:'Langs',其中包含一个attribut'title'(例如:Español)和一个属性代码(Ex:es_ES)。

BDD架构是强加的,不可改变的......(对我来说不好!)。这两个实体之间没有链接(表..)。

我想为其中的字段使用的模型创建一个编辑表单:

  • 是一个多选复选框
  • 以标题显示(在Langs实体中为attribut title)
  • 存储在模型中,如es_ES,us_US(等等,如果用户检查多种语言)

我的文件ModelsType:

class ModelsType extends \MyProject\AdminBundle\Form\Type\ModerationAbstractType
{

   /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
      // $spokenlangTransformer = new SpokenLangsTransformer($this->entityManager);
        $builder


            ->add( $this->getLangsField( $builder, 'spokenlangs', array('multiple' => true, 'expanded' => true) ))

            ->add( 'user', new UserType($this->entityManager) )
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyProject\EntityBundle\Entity\Models'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'myproject_entitybundle_models';
    }
}

我的ModerationAbstractType文件(其中定义了getLangsField())

namespace MyProject\AdminBundle\Form\Type;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use MyProject\AdminBundle\Form\Transformer\CountryTransformer;
use MyProject\AdminBundle\Form\Transformer\SpokenLangsTransformer;
/**
 * Centralyze user form type
 */
abstract class ModerationAbstractType extends AbstractType
{
    /** @var EntityManager */
    protected $entityManager;

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


    /**
     * Return a lang field linked to the langs list by code
     * @param type $name
     * @param array $options
     * @return type
     */
    public function getLangsField($builder, $name, $options){

        $transformer = new SpokenLangsTransformer($this->entityManager);

        return $builder->create($name, 'choice', $options)
                       ->addModelTransformer($transformer);
    }


}

我的SpokenLangsTransformer文件:

namespace MyProject\AdminBundle\Form\Transformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
use MyProject\EntityBundle\Entity\Wmlangs;

/**
 * Description of SpokenLangsTransformer
 *
 * 
 */
class SpokenLangsTransformer implements DataTransformerInterface {

    /**
     * @var ObjectManager
     */
    private $om;

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

    /**
     * Transforms an object (wmlangs) to a string (code).
     *
     * @param  Wmlangs|null $spokenlangs
     * @return string
     */
    public function transform($spokenlangs) {


        if (null === $spokenlangs) {
            return "";
        }
        $codeArray = array_filter(explode(",", $spokenlangs));

        foreach ($codeArray as $code) {
            $spokenlangsArray[] = $this->om
                               ->getRepository('MyProject\EntityBundle\Entity\Wmlangs')
                               ->findOneBy(array('code' => $code));

        }

        foreach($spokenlangsArray as $namelang) {
           $namesLangs[] =  $namelang->getTitle();
        }

         return $namesLangs;
    }

    /**
     * Transforms a string (number) to an object (issue).
     *
     * @param  string $number
     * @return Wmlangs|null
     * @throws TransformationFailedException if object (wmlangs) is not found.
     */
    public function reverseTransform($codes) {
        if (!$codes) {
            return null;
        }

         $codeArray = array_filter(explode(",", $codes));

        foreach ($codeArray as $code) {

        $spokenlangs[] = $this->om
                               ->getRepository('MyProject\EntityBundle\Entity\Wmlangs')
                               ->findOneBy(array('code' => $code));

        }
        if (null === $spokenlangs) {
            throw new TransformationFailedException(sprintf(
                    'Le problème avec le code "%s" ne peut pas être trouvé!', $code
            ));
        }

        return $spokenlangs;
    }

}

使用此实际代码,该字段不显示任何内容..

请问,我怎样才能添加我期望的内容?

注意:请注意,当我尝试访问表单时,它会传递转换函数(在我的datatransformer中......我认为这不对)

1 个答案:

答案 0 :(得分:0)

尝试在getLangsField函数

提交的'entity'表单