如您所知,所有上帝的造物都是不同的 。它们最适合像 mongodb 这样的非关系数据库。假设我有一个带有不同 ZooCollecion
文档对象的 Animal
。
我如何使用 sonata-admin 使用简单的“类型”选择菜单(或相关内容)更改文档类 ?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 }
动物 -
Acme/DemoBundle/Document/Animal
( 基础文档 ):/**
* 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
(扩展基础文档):/**
* Class representing Elephants
*/
class Elephant extends Animal
{
...
}
海龟 -
Acme/DemoBundle/Document/Turtle
(扩展基础文档):/**
* Class representing Turtles
*/
class Turtle extends Animal
{
...
}
最佳答案
管理中这种类型的实现的一个很好的例子是 sonata ecommerce 中的产品创建管理
(请参阅 ProductAdminController 中的 createAction: https://github.com/sonata-project/ecommerce/blob/master/src/ProductBundle/Controller/ProductAdminController.php )。
基本上我们所做的是通过服务定义(参见 https://github.com/sonata-project/ecommerce/blob/master/src/ProductBundle/Resources/config/admin.xml )覆盖 AdminController,这允许我们覆盖 createAction 以从类型选择开始,然后根据这个参数编辑表单(这里不是一个类型,但是产品提供商;但这基本上是同一件事)。
关于php - 奏鸣曲管理员 : Use multiple document classes (types),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20494185/