Daniel Pysarenko
Daniel Pysarenko

Reputation: 41

Slim 3 + Doctrine 2: Class 'User' does not exist, 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..

Upvotes: 0

Views: 939

Answers (1)

Daniel Pysarenko
Daniel Pysarenko

Reputation: 41

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;

Upvotes: 1

Related Questions