【问题标题】:ODM inheritance document with type SINGLE_COLLECTION schema create errorSINGLE_COLLECTION 模式类型的 ODM 继承文档创建错误
【发布时间】:2020-05-05 14:24:54
【问题描述】:

我在 Symfony 4.4 应用程序中配置了类型为 SINGLE_COLLECTION 的继承文档。 当我运行命令bin/console doctrine:mongodb:schema:create 时,出现错误a collection 'db.Person' already exists

一切都按照文档完成:https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/2.0/reference/inheritance-mapping.html#single-collection-inheritance

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 exists Created indexes for all classes

标签: php mongodb symfony doctrine doctrine-odm


【解决方案1】:

ODM 的odm:schema:create 正在遍历所有元数据并尝试创建一个集合,而不考虑它们之间可能存在的关系。在 ODM 的 SchemaManager 中进行适当的修复,以在创建或捕获异常之前检查集合是否存在,并在存在集合的情况下忽略它。

我创建了https://github.com/doctrine/mongodb-odm/issues/2182 来跟踪这个问题。如果您有空闲时间,我们将不胜感激!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2012-12-17
    • 2016-08-19
    • 2014-08-08
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多