【问题标题】:The class 'App\Document\User' was not found in the chain configured namespaces在链配置的命名空间中找不到类“App\Document\User”
【发布时间】:2018-11-26 00:09:00
【问题描述】:

我刚刚用 symfony4.1 安装了 mongodb。

当我想在数据库中持久化一个简单的用户时,我得到了这个错误(标题):

未捕获的 PHP 异常 Doctrine\Common\Persistence\Mapping\MappingException: "在链配置的命名空间中找不到类 'App\Document\User'

这里是我的控制器:

/**
 * @Route("/mongoTest")
 * @Method("GET")
 * @param DocumentManager $dm
 * @return JsonResponse
 */
public function mongoTest(DocumentManager $dm)
{
    $user = new User();
    $user->setEmail("hello@medium.com");
    $user->setFirstname("Matt");
    $user->setLastname("Matt");
    $user->setPassword(md5("123456"));
    $dm->persist($user);
    $dm->flush();
    return new JsonResponse(array('Status' => 'OK'));
}

配置:

doctrine_mongodb:
  connections:
    default:
      server: "%mongodb_server%"
      options: {}
  default_database: "%mongodb_database_name%"
  document_managers:
    default:
      auto_mapping: true
  default_commit_options: ~

这是我的文档:

<?php

namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
 * @MongoDB\Document
 */
class User
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $firstname;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $lastname;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $email;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $password;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $create_date;

    // ...
}

有人有想法吗?

非常感谢!!

【问题讨论】:

    标签: php mongodb symfony4


    【解决方案1】:

    我找到了答案:

    我不使用捆绑包。

    doctrine_mongodb:
      connections:
        default:
          server: "%mongodb_server%"
          options: {}
      default_database: "%mongodb_database_name%"
      document_managers:
        default:
          mappings:
            # ...
            App:
              type: annotation
              dir: "%kernel.root_dir%/../src/Document"
              is_bundle: false
              prefix: App\Document
              alias: App
      default_commit_options: ~

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 2019-08-17
      • 2019-08-23
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      相关资源
      最近更新 更多