As you know, all God's creatures are different. They best fit into a non-relational database like mongodb. Let's say I have a ZooCollecion
with different Animal
Document Objects.
How can I use sonata-admin, 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;
...
}
Elephant - Acme/DemoBundle/Document/Elephant
(extends the base document):
/**
* Class representing Elephants
*/
class Elephant extends Animal
{
...
}
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).