【发布时间】:2020-05-05 14:24:54
【问题描述】:
我在 Symfony 4.4 应用程序中配置了类型为 SINGLE_COLLECTION 的继承文档。
当我运行命令bin/console doctrine:mongodb:schema:create 时,出现错误a collection 'db.Person' already exists。
src/Document/Person.php
<?php
namespace App\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document
* @MongoDB\InheritanceType("SINGLE_COLLECTION")
* @MongoDB\DiscriminatorField("type")
* @MongoDB\DiscriminatorMap({"person"=Person::class, "employee"=Employee::class})
*/
class Person
{
/**
* @var integer|null
*
* @MongoDB\Id
*/
protected $id;
/**
* @var string|null
*
* @MongoDB\Field(type="string")
*/
protected $name;
}
src/Document/Employee.php
<?php
namespace App\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document
*/
class Employee extends Person
{
/**
* @var string|null
*
* @MongoDB\Field(type="string")
*/
protected $grade;
}
看起来命令正在尝试为每个文档类创建数据库集合,忽略 SINGLE_COLLECTION 类型的声明。
如何解决?
【问题讨论】:
-
MongoDB 中的集合不需要显式创建。失败的操作到底是什么?
-
我知道它不是必需的,但我更喜欢为索引创建完整的数据库模式。扩展错误响应为:
MongoDB\Driver\Exception\CommandException: a collection 'db.Person' already exists in vendor/mongodb/mongodb/src/Operation/CreateCollection.php:222 -
什么是完整的堆栈跟踪?
-
奥列格,我无法获取堆栈跟踪。当我运行
bin/console doctrine:mongodb:schema:create -vvv时,我得到了回复:a collection 'db.Person' already existsCreated indexes for all classes
标签: php mongodb symfony doctrine doctrine-odm