简体   繁体   中英

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. I would like to add a new field to Sonata's Post entity (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 . 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? How can i do this?

Thank you in advance.


PS: I have attached a standard Sonata Admin screenshot with existing "enabled" field . 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. 在此处输入图片说明

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. Documentation

Also, you can generate Admin class from existing entity:

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. 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.

Options are:

bundle : the bundle name (the default value is determined by the given model class, eg “YourNSFooBundle”)

admin : the admin class basename (by default this adds “Admin” to the model class name, eg “BarAdmin”)

controller : the controller class basename (by default this adds “AdminController” to the model class name, eg “BarAdminController”)

manager : the model manager type (by default this is the first registered model manager type, eg “orm”)

services : the services YAML file (the default value is “services.yml” or “admin.yml” if it already exist)

id : the admin service ID (the default value is combination of the bundle name and admin class basename like “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')
   ;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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