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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)