使用 Sonata Admin 显示字段的问题 - Symfony 2.0 [英] Problems showing fields with Sonata Admin - Symfony 2.0

查看:26
本文介绍了使用 Sonata Admin 显示字段的问题 - Symfony 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 SonataAdmin 显示对象的一些字段.其中一个字段是数据库中的整数(状态),但我不想显示该整数,否则根据该字段的值显示特定字符​​串.

public 函数 configureListFields(ListMapper $listMapper){$listMapper->addIdentifier('name')->add('通知')->add('开始')->add('end')->add('状态');}

有可能吗?

还有第二个问题:在同一个示例中,我想添加一个未映射到数据库(人员)中的字段,因为这是使用与其他对象相关的数据计算的.

public 函数 configureListFields(ListMapper $listMapper){$listMapper->addIdentifier('name')->add('通知')->add('开始')->add('end')->add('状态')->add('人');}

我可以使用 Sonata Admin 执行此操作吗?

提前致谢.

解决方案

感谢您的回答.但是,我一直在 Internet 上寻找信息,并且找到了其他方法.您可以为您的字段呈现特定模板.在您的 Admin 类中,您可以在 configureListFields 函数中执行以下操作:

public 函数 configureListFields(ListMapper $listMapper){$listMapper->addIdentifier('name')->add('通知')->add('开始')->add('end')->add('status', 'string', array('template' => 'YourBundle:YourController:status_field.html.twig'))->add('Resources', 'string', array('template' => 'YourBundle:YourController:resources_field.html.twig'))}

对于状态"字段,status_field.html.twig 模板将呈现如下:

{% 扩展 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}{% 块字段 %}<div>{% if object.status == 0 %}<strong>Inactivo</strong>{% elseif object.status == 1 %}<strong>Activo</strong>{% 万一 %}

{% 结束块 %}

对于我的第二个问题,同样的解决方案是有效的.为资源"字段呈现的模板如下:

{% 扩展 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}{% 块字段 %}<div><strong>{{object.getResources|length}}</strong>

{% 结束块 %}

这样,对象就传递给了模板,你可以使用它的方法来获取你需要的信息.在本例中,方法 getResources() 用于显示其长度.

I want to show some fields of an object with SonataAdmin. One of these fields is an integer (status) in the database, but I don't want to show the integer, else a specific string depending on the value of this field.

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('notice')
        ->add('start')
        ->add('end')
        ->add('status')            
    ;
}

Is it possible?

And a second question: in the same example, I want to add a field which is not mapped in the database (people) because this is calculated with data related with other objects.

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('notice')
        ->add('start')
        ->add('end')
        ->add('status')
        ->add('people')         
    ;
}

Can I do this with Sonata Admin?

Thanks in advance.

解决方案

Thanks for the answer. However, I've been looking for information on Internet and I've found other way to do this. You can render a specific template for your field. In your Admin class, in the configureListFields function you could do this:

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('notice')
        ->add('start')
        ->add('end')
        ->add('status', 'string', array('template' => 'YourBundle:YourController:status_field.html.twig'))
        ->add('Resources', 'string', array('template' => 'YourBundle:YourController:resources_field.html.twig'))
}

For the "status" field, the status_field.html.twig template will be rendered as below:

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

{% block field %}
    <div>
        {% if object.status == 0 %} 
            <strong>Inactivo</strong>
        {% elseif object.status == 1 %}
            <strong>Activo</strong>
        {% endif %}
    </div>
{% endblock %}

And for my second question, the same solution is valid. The template rendered for the "resources" field would be as below:

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

{% block field %}
    <div>
        <strong>{{ object.getResources|length }}</strong>
    </div>
{% endblock %}

In this way, the object is passed to the template, and you can use its methods to get the information that you need. In this case, the method getResources() is used to display its length.

这篇关于使用 Sonata Admin 显示字段的问题 - Symfony 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆