0
votes

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
Looks like you missing import for annotations in your User entity class. It was something like "use Doctrine\ORM\Mapping;"Eakethet
Eakethet, is it must have? because i don't use themDaniel Pysarenko
Actually, you use them in annotations... @Column and etc., its goes all from this ORM\MappingEakethet
allready tried to add "use Doctrine\ORM\Mapping" + all "@" in DocBlock changed into "@ORM\" instance: "@Entity" -> "@ORM\Entity". The problem didn't solveDaniel Pysarenko
if youve tried @ORM\Entity, you must declare the use statement as "use Doctrine\ORM\Mapping as ORM;", did you?Eakethet

1 Answers

1
votes

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;