【问题标题】:Slim 3 + Doctrine 2: Class 'User' does not exist, MappingExceptionSlim 3 + Doctrine 2:“用户”类不存在,MappingException
【发布时间】:2018-04-20 08:23:37
【问题描述】:

我使用 Slim 3 和 Doctrine 2。

我无法通过其他stackoverflow问题解决这个问题,在goolge中搜索也无济于事......

我在注册时尝试保存用户,但是

Slim 应用程序错误应用程序无法运行,因为 以下错误:详细信息类型: Doctrine\Common\Persistence\Mapping\MappingException 消息:类 “用户”不存在文件: C:\wamp\www\slimproject\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php 线路:96

我的用户类

!!!当我删除“命名空间 App\Models;”行时它工作正常,我认为这是一个问题..

<?php

namespace App\Models;

/**
 * @Entity
 * @Table(name="users")
 */
class User
{
/**
 * @id
 * @Column(type="integer")
 * @GeneratedValue(strategy="AUTO")
 *
 */
public $id; ect

/**
 * @name
 * @Column(type="string")
 */
public $name; ect...

AuthController:

use App\Models\User;
...

$user = new User();
    $user->setEmail($request->getParam('email'));
    $user->setName($request->getParam('name'));
    $user->setPassword(password_hash($request->getParam('password'), PASSWORD_DEFAULT) );
    $user->created_at();
    $user->updated_at();

    $db = new Doctrine();
    $db->em->persist($user);
    $db->em->flush();

我的 composer.json

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Tests\\": "tests/"
    }
}

教义配置

$paths = array('/app/Models/');
    $isDevMode = false;
    $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);

    $connectionOptions = array(
        'driver'   => 'pdo_mysql',
        'host'     => 'localhost',
        'dbname'   => 'monday',
        'user'     => 'root',
        'password' => '',
    );

    $this->em = EntityManager::create($connectionOptions, $config);

文件夹结构 (WAMP)

www/
---slimproject/
--------------app/
-----------------Controllers/
----------------------------Auth/AuthController.php
-----------------Models/User.php
--------------public/
--------------composer.json

希望有人帮忙解决。

【问题讨论】:

  • 看起来您缺少用户实体类中注释的导入。类似于“使用 Doctrine\ORM\Mapping;”
  • Eakethet,是必须的吗?因为我不使用它们
  • 其实你在注解中使用它们... @Column 等等,都来自这个 ORM\Mapping
  • 已经尝试添加“使用 Doctrine\ORM\Mapping”+DocBlock 中的所有“@”更改为“@ORM\”实例:“@Entity”->“@ORM\Entity”。问题没有解决
  • 如果你尝试过@ORM\Entity,你必须声明use语句为“use Doctrine\ORM\Mapping as ORM;”,是吗?

标签: php database doctrine-orm orm slim-3


【解决方案1】:

问题解决了!

当我使用方法学说 getRepository() 时,参数是 ('User'),我改为 (UserEntity::class) 预添加使用 App\Models\User 作为 UserEntity;

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多