doushuo1080 2018-04-20 08:23
浏览 56

Slim 3 + Doctrine 2:Class'User'不存在,MappingException

I use Slim 3 with Doctrine 2.

I can't solve this problem by other stackoverflow questions, searching in goolge also doesnt help...

I tried to save user during registration, but

Slim Application Error The application could not run because of the following error: Details Type: Doctrine\Common\Persistence\Mapping\MappingException Message: Class 'User' does not exist File: C:\wamp\www\slimproject\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php Line: 96

My User class

!!! When i remove the line "namespace App\Models;" it works fine, i think here is a problem..

<?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();

my composer.json

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

Doctrine configuration

$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);

Folder structure (WAMP)

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

I hope somebody help to fix it..

  • 写回答

1条回答 默认 最新

  • doufang7385 2018-04-21 08:55
    关注

    The problem solved!

    When i used method doctrine getRepository(), the param was ('User'), i changed to (UserEntity::class) pre-adding use App\Models\User as UserEntity;

    评论

报告相同问题?

悬赏问题

  • ¥15 vscode问题请教
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM