0

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

我有一个实体:'Models',其中包含一个属性 'spokenlangs' 格式: ,es_ES,fr_FR,

我有一个实体:“Langs”,其中包含一个属性“标题”(例如:Español)和一个属性代码(例如:es_ES)。

BDD 模式是强加的并且不可更改..(对我来说很糟糕!)。这两个实体(表..)之间不存在链接。

我想为字段 speaklangs 的模型创建一个编辑表单:

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

我的文件模型类型:

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;
    }

}

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

请问,我该怎么做才能添加我的期望?

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

4

1 回答 1

0

尝试在 getLangsField 函数中提交的“实体”表单

于 2014-04-14T15:37:50.877 回答