Pregunta

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) ?

Sonata Admin - Demonstrate a "type" select-menu

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;

    ...
}

Elephant Icon - (c) VisualPharm visualpharm.com Elephant - Acme/DemoBundle/Document/Elephant (extends the base document):

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

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

/**
 * Class representing Turtles
 */
class Turtle extends Animal
{
    ...
}
¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top