【问题标题】:Symfony2 - Doctrine Entity array field and formsSymfony2 - Doctrine Entity 数组字段和表单
【发布时间】:2013-11-21 20:51:51
【问题描述】:

假设我有一个具有 $title 字段(数组类型)的 Post 实体,并且我希望允许用户以多种语言编写帖子的标题

/**
 * Post
 *
 * @ORM\Table(name="posts")
 * @ORM\Entity
 */
class Post
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var array
     *
     * @ORM\Column(name="title", type="array", nullable=true)
     */
    protected $title;
}

当用户想要提交新帖子时,如何创建生成这些字段的表单类型?

<input type="text" name="title[en]" />
<input type="text" name="title[fr]" />

【问题讨论】:

  • 我真的不认为这是用学说实现翻译行为的好方法。为什么不使用 DoctrineExtension 可翻译?

标签: php symfony doctrine-orm


【解决方案1】:

你必须为你的表单创建一个子类型:

class TitleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $builder
                    ->add('en')
                    ->add('fr');
    }

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

然后你可以在你的主类型中添加这个子类型:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $builder
                    ->add('title', new TitleType());
    }

【讨论】:

    猜你喜欢
    • 2014-03-31
    • 2015-06-16
    • 2014-04-04
    • 2012-08-06
    • 2014-01-03
    • 2014-08-23
    • 1970-01-01
    • 2014-01-05
    • 2013-07-29
    相关资源
    最近更新 更多