【问题标题】:Symfony 2 | Sonata : collection remove numbersSymfony 2 |奏鸣曲:收集删除数字
【发布时间】:2014-09-26 10:15:42
【问题描述】:

我现在正在寻找几天。 我的管理实体中有一个集合,一切正常,我得到:

我想删除这个数字(这里是 0 和 1)。

我的新闻管理员

$formMapper
        ->add('comments', 'collection', array('type' => new ...\NewsCommentType(),
                                                    'allow_add'    => false,
                                                    'allow_delete' => false,
                                                    'by_reference' => false,
                                                    "required" => false,
                                                    "label" => "Comments"))
    ;

还有我的 NewsCommentType :

class NewsCommentType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('content', null, array(
            "disabled" => true,
            "label" => false)
        )
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => '...\NewsComment'
    ));
}

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

还有我的 edit_news.html.twig :

{% extends 'SonataAdminBundle:CRUD:edit.html.twig' %}

{% block javascripts %}
    {{ parent() }}
    <script src="{{ asset("bundles/.../js/ckeditor/ckeditor.js") }}"></script>
    <script type="text/javascript">

    </script>
{% endblock %}

{% block stylesheets %}
    {{ parent() }}
{% endblock %}


{% block formactions %}
<div class="well well-small form-actions">
    <button type="submit" class="btn btn-success" name="btn_update_and_edit"><i class="fa fa-save"></i> {{ 'btn_update_and_edit_again'|trans({}, 'SonataAdminBundle') }}</button>
    <button type="submit" class="btn btn-success" name="btn_update_and_list"><i class="fa fa-save"></i> <i class="fa fa-list"></i> {{ 'btn_update_and_return_to_list'|trans({}, 'SonataAdminBundle') }}</button>
    <a class="btn btn-success" href="{{ admin.generateUrl('list') }}">
    <i class="fa fa-list"></i>
    {{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}</a>
</div>
{% endblock formactions %}

在奏鸣曲文档中没有任何内容(或者我错过了它)所以我不知道从哪里开始。 是否有 collection 的属性?我必须用javascript来做?

【问题讨论】:

  • 你能回答或争论而不是投票吗? TY ...难以置信的Oo
  • 你能指出你使用的文件的方向吗,我看看能不能帮到你
  • 嗨,TY 我遵循了一些这样的例子:symfony.com/doc/current/cookbook/form/form_collections.html。但我用我的代码编辑了我的帖子。我认为这是因为奏鸣曲,但我不知道为什么。
  • 好的,我已经设法使用碎片化原则删除了数字,但为了确保这不会太复杂,我可以请你也张贴你的树枝标记,因为可能有一个更简单的解决方案更易于维护
  • 哇,你的帮助,我用树枝编辑了我的帖子。我仍然找不到解决方案。

标签: symfony sonata-admin symfony-sonata


【解决方案1】:

我找到了一个解决方案,我发布了它,即使有些人不加争论地对我的帖子投了反对票(如果我的问题对你来说太难了,请通过 TY)

这个数字是使用奏鸣曲和类型集合的结果。 您必须将 sonata_type_collection 与您的自定义类型一起使用并设置“编辑”和“内联”值(仅适用于 sonata_type_collection)来自定义您的结果:

在您的管理员课程中:

$formMapper
        ->add('comments', 'sonata_type_collection', array('type' => new ...\NewsCommentType(), 
            'by_reference' => true,
            'required' => false),     
            array(
                'edit' => 'inline',
                'inline' => 'table',
                'sortable' => 'id')
            );
    ;

并且在你的 AbstractType 中,你可以显示你想要的字段:

 $builder
    ->add('content', null, array(
        "disabled" => true,
        "label" => false)
    )
;

结果并不完美,但没有更多的数字;)

感谢那些试图帮助我的人 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-24
    • 2018-01-09
    • 2019-08-11
    • 1970-01-01
    • 2018-02-24
    • 2018-10-15
    • 2018-03-22
    • 1970-01-01
    相关资源
    最近更新 更多