【问题标题】:Symfony2 collection of entities, add new from selectSymfony2 实体集合,从选择中添加新的
【发布时间】:2014-08-07 19:26:41
【问题描述】:

我从 Symfony2 开始,如果我的问题很简单,请原谅:

我有 2 个实体:

新闻(id、标题、标签)

标签(id、新闻)

我有多对多的关系

新闻示例代码:

/**
  * @ORM\ManyToMany(targetEntity="Tag", inversedBy="news", cascade={"persist", "remove"}))
  * @ORM\JoinTable(name="news_tags")
  */
  private $tags;

标签示例代码:

/**
 * @ORM\ManyToMany(targetEntity="News", mappedBy="tags")
 */
 private $news;

我有一个 TagType 可以给 DB 添加标签,很简单

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('name', 'text');
}

新闻类型

public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('title', 'text', array(
                    'attr'=>array(
                        'maxlength'=>60
                    )
                ))
                ->add('tags', 'collection', array(
                    'type'=>new TagType(),
                    'allow_add'=>true,
                    'allow_delete'=>true,
                    'by_reference'=>false,
                ))
        ;
    }

新闻表单 HTML 示例:

<table class="tags table table-hover">
            <thead>
                <tr>
                    <th>name</th>
                    <th>action</th>
                </tr>
            </thead>
            <tbody data-prototype="{% filter escape %}{% include 'XAdminBundle:Form:news_tags_prototype.html.twig' with {'form': form.tags.vars.prototype} %}{% endfilter %}">
                {% for tag in form.tags %}
                    <tr>
                        <td>
                            {{ tag.vars.data.name }}
                            {{ form_widget(tag.name, {'attr': {'class': 'hidden'} }) }}
                        </td>
                        <td class="action">                         
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

news_tags_prototype.html.twig

<tr>
    <td>{{ form_widget(form.name, {'attr': { 'class': 'form-control' } }) }}</td>
    <td class="action"></td>
</tr>

在这种情况下,当我单击添加标签时,我有一个文本输入,我可以添加全新的标签,但是在单击添加标签后我想要的唯一一个选择框,其中包含来自 DB 的标签实体的名称。 如何做到这一点 throw js 和原型(由http://symfony.com/doc/current/cookbook/form/form_collections.html 建议)。

感谢您的帮助!

【问题讨论】:

    标签: symfony select collections prototype entities


    【解决方案1】:

    在 TagType 中,您应该使用以下内容:

    $builder->add('name', 'entity', array(
    'class' => 'SomeBundle:Tag'));
    

    这将确保原型呈现为下拉菜单而不是文本元素。希望对您有所帮助。

    编辑:我刚刚意识到您可能首先需要现有的 TagType 才能将标签添加到数据库中。在这种情况下,使用上面我建议的更改创建一个名为 TagDropdownType 的新文件,然后在您的 NewsType 中更改:

    ->add('tags', 'collection', array(
                    'type'=>new TagType(),
                    'allow_add'=>true,
                    'allow_delete'=>true,
                    'by_reference'=>false,
                ))
    

    到:

    ->add('tags', 'collection', array(
                    'type'=>new TagDropdownType(),
                    'allow_add'=>true,
                    'allow_delete'=>true,
                    'by_reference'=>false,
                ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 2013-08-22
      • 2012-02-13
      • 1970-01-01
      相关资源
      最近更新 更多