简体   繁体   English

Sonata管理员:向发布实体添加新字段

[英]Sonata Admin: Add new field to Post Entity

I am planning to use Sonata Admin for one of my projects, so for now i am testing its features. 我打算将Sonata Admin用于我的一个项目,所以现在我正在测试其功能。 I would like to add a new field to Sonata's Post entity (SonataNewsBundle). 我想向Sonata的Post实体(SonataNewsBundle)添加一个新字段。

For example I want to add a new check-box (called "breaking"), for news marked as Breaking news. 例如,我想为标记为“突发新闻”的新闻添加一个新复选框(称为“突发”)。

Sonata Admin is a great piece of software, but it really lacks documentation . Sonata Admin是一款很棒的软件,但实际上缺少文档 I've looked to docs on the official site, but i have no clue what should I do to accomplish this task. 我已经看过官方网站上的文档,但是我不知道我应该怎么做才能完成此任务。

What are the steps to add a new boolean field (breaking) to Sonata Admin Post entity? 向Sonata Admin Post实体添加新的布尔字段(中断) 的步骤是什么? How can i do this? 怎样才能做到这一点?

Thank you in advance. 先感谢您。


PS: I have attached a standard Sonata Admin screenshot with existing "enabled" field . PS:我已经在标准的Sonata Admin屏幕快照中添加了现有的“ enabled”字段 I would like to have a similar field for news marked as breaking . 我想在新闻领域也有类似的标记为“突发事件” I do not need to use the tag (or any other classification system) or the category based classification - just a check-box that would mark or unmark the Post entity as breaking. 我不需要使用标签(或任何其他分类系统)或基于类别的分类-只需使用一个复选框即可将Post实体标记或取消标记为断开。 在此处输入图片说明

You have to add your boolean field to your entity 您必须将布尔字段添加到实体

    /**
     * @ORM\Column(type="bool")
     */
    protected $is_visible;

and add the field to your admin form mapper and set apropriate field type, for example: 并将字段添加到您的管理表单映射器中,并设置适当的字段类型,例如:

$formMapper
            ->add('is_visible', 'bool', array(
                'label' => 'Title'
            ))

If no type is specified, SonataAdminBundle tries to guess it. 如果未指定任何类型,则SonataAdminBundle会尝试猜测。 Documentation 文档

Also, you can generate Admin class from existing entity: 另外,您可以从现有实体生成Admin类:

php app/console sonata:admin:generate YourNS/FooBundle/Entity/Bar

The sonata:admin:generate command generates a new Admin class based on the given model class, registers it as a service and potentially creates a new controller. sonata:admin:generate命令根据给定的模型类生成一个新的Admin类,将其注册为服务,并可能创建一个新的控制器。 As an argument you need to specify the fully qualified model class. 作为参数,您需要指定完全限定的模型类。 All passed arguments and options are used as default values in interactive mode. 在交互模式下,所有传递的参数和选项均用作默认值。 You can disable the interactive mode with --no-interaction option. 您可以使用--no-interaction选项禁用交互模式。

Options are: 选项有:

bundle : the bundle name (the default value is determined by the given model class, eg “YourNSFooBundle”) bundle :捆绑包名称(默认值由给定的模型类确定,例如“ YourNSFooBundle”)

admin : the admin class basename (by default this adds “Admin” to the model class name, eg “BarAdmin”) admin :管理员类的基本名称(默认情况下,这会在模型类名称中添加“ Admin”,例如“ BarAdmin”)

controller : the controller class basename (by default this adds “AdminController” to the model class name, eg “BarAdminController”) controller :控制器类的基本名称(默认情况下,这会在模型类名称中添加“ AdminController”,例如“ BarAdminController”)

manager : the model manager type (by default this is the first registered model manager type, eg “orm”) manager :模型管理员类型(默认情况下,这是第一个注册的模型管理员类型,例如“ orm”)

services : the services YAML file (the default value is “services.yml” or “admin.yml” if it already exist) services :services YAML文件(默认值为“ services.yml”或“ admin.yml”,如果已存在)

id : the admin service ID (the default value is combination of the bundle name and admin class basename like “your_ns_foo.admin.bar”) id :管理员服务ID(默认值是捆绑软件名称和管理员类基本名称的组合,例如“ your_ns_foo.admin.bar”)

"--- > You have to add your boolean field to your entity Where i should add this field" For example: “ --->您必须将布尔字段添加到您的实体,我应该在其中添加此字段”例如:

// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\Column(type="decimal", scale=2)
     */
    protected $price;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;

   /**
    * @ORM\Column(type="bool")
    */
   protected $isVisible;
}

In your Admin class: 在您的管理员课程中:

<?php
// src/AppBundle/Admin/ProductAdmin.php

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;

class ProductAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('name', 'text', array(
                'label' => 'Product Name'
            ))
            ->add('price', 'decimal', array(
                'label' => 'Product Price'
            ))

            // if no type is specified, SonataAdminBundle tries to guess it
            ->add('description')
            ->add('isVisible', 'bool', array(
                'label' => 'Is Product visible'
            ))

            // ...
       ;
    }
    // Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
   $datagridMapper
        ->add('name')
        ->add('price')
   ;
}

// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('price')
   ;
}

// Fields to be shown on show action
protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
       ->add('name')
       ->add('price')
       ->add('isVisible')
   ;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM