简体   繁体   中英

Sonata Admin: Use multiple document classes (types)

As you know, all God's creatures are different . They best fit into a non-relational database like . Let's say I have a ZooCollecion with different Animal Document Objects.

How can I use , to change the document class with a simple "type" select-menu (or something related) ?

奏鸣曲管理员 - 演示“类型”选择菜单

Acme/DemoBundle/Resources/config/services.yml

services:
    animal.admin.page:
        class: %animal.admin.class%
        arguments: [null, %animal.class%, null] # Is this the key ?
        calls:
            - [ setContainer, [ @service_container ] ]
        tags:
            - { name: sonata.admin, manager_type: doctrine_mongodb, group: Zoo, label: Animals }

Animal - Acme/DemoBundle/Document/Animal ( base document ):

/**
 * Class representing Animals
 *
 * @MongoDB\Document(collection="zoo_animals", 
 *  repositoryClass="Acme\DemoBundle\Repository\ZooRepository")
 */
class Animal
{
    /**
     * @MongoDB\Id(strategy="auto")
     */
    protected $id;

    /**
     * @MongoDB\Hash
     * @Assert\NotBlank(message="Type type should not be blank.")
     */
    protected $type;

    ...
}

大象图标 - (c)VisualPharm visualpharm.com Elephant - Acme/DemoBundle/Document/Elephant ( extends the base document ):

/**
 * Class representing Elephants
 */
class Elephant extends Animal
{
    ...
}

龟图标 - (c)VisualPharm visualpharm.com Turtle - Acme/DemoBundle/Document/Turtle ( extends the base document ):

/**
 * Class representing Turtles
 */
class Turtle extends Animal
{
    ...
}

A good example of that type of implementation in the admin would be the product creation admin in sonata ecommerce (see the createAction in the ProductAdminController: https://github.com/sonata-project/ecommerce/blob/master/src/ProductBundle/Controller/ProductAdminController.php ).

Basically what we did was override the AdminController through the service definition (see https://github.com/sonata-project/ecommerce/blob/master/src/ProductBundle/Resources/config/admin.xml ), which allowed us to override the createAction to begin with a type selection, and then edit the form according to this argument (here it's not a type per say, but a product provider ; but this basically is the same thing).

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