ciro
ciro

Reputation: 801

Zend framework 3 - Doctrine entity not found

The purpose is to create a module in which to insert all the entities and then use them in the other modules. This is why I created a module called Entity in which I entered the entities. Unfortunately when I try to use one of these entities within the basic controller I get an error: class not found

This is my composer

"autoload": {
        "psr-4": {
            "Application\\": "module/Application/src/",
            "Entity\\": "module/Entity/src/"
        }
    },

I already execute this command

composer dump-autoload      

My entity class is under

module/Entity/src/Model/    

This is my class

<?php

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * SysUserRole
 *
 * @ORM\Table(name="sys_user_role")
 * @ORM\Entity
 */
class SysUserRole
{
    /**
     * @var int
     *
     * @ORM\Column(name="ID_SYS_USER_ROLE", type="integer", nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idSysUserRole;

    /**
     * @var string
     *
     * @ORM\Column(name="NAME", type="string", length=100, nullable=false)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="DESCRIPTION", type="string", length=255, nullable=false)
     */
    private $description;

    /**
     * @return int
     */
    public function getIdSysUserRole()
    {
        return $this->idSysUserRole;
    }

    /**
     * @param int $idSysUserRole
     */
    public function setIdSysUserRole($idSysUserRole)
    {
        $this->idSysUserRole = $idSysUserRole;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param string $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }


}

This is my config\autoload\doctrine.global.php

 'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => PDOMySqlDriver::class,
                'params' => [
                    'host'     => 'localhost',
                    'user'     => '***',
                    'password' => '****',
                    'dbname'   => 'mydb',
                    'charset'  => 'utf8',
                    'driverOptions' => array(
                        1002 => 'SET NAMES utf8'
                    )
                ]
            ],
        ],
        'authentication' => [
            'orm_default' => [
                'object_manager' => 'Doctrine\ORM\EntityManager',
                'identity_class' => 'Application\Entity\User',
                'identity_property' => 'email',
                'credential_property' => 'password',
            ],
        ],
        'driver' => [
            'entity_driver' => [
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => [__DIR__ . '/../../module/Entity/src/Model/']
            ],
            'orm_default' => [
                'drivers' => [
                    '\Entity' => 'entity_driver'
                ]
            ]
        ],
    ],

In controller action i try this

$entity = new SysUserRole();    

The error is

Class 'Entity\SysUserRole' not found    

edit:

the error is changed

I move this code inside a module.config.php

'doctrine' => [
        'driver' => [
            __NAMESPACE__ . '_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(__DIR__ . '/../src/Model')
            ),
            'orm_default' => [
                'drivers' => [
                    //was \Entity
                    'Entity\Model' =>  __NAMESPACE__ . '_driver'
                ]
            ]
        ],
    ]

new error is

    The class 'Entity\Model\SysUserRole' was not found in the chain configured namespaces \Entity\

Upvotes: 0

Views: 652

Answers (1)

rkeet
rkeet

Reputation: 3468

Folder structure and psr-4 registered namespace. I would expect the Entity namespace to be Entity\Model with then the class name.

Btw, using a module for a purpose would probably be a better approach. Having Entities per module, for the purpose of the module (e.g. User and Profile Entity objects in the User module), makes sense.

namespace SomeSpace

use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

return [
    'doctrine' => [
        'driver' => [
            __NAMESPACE__ . '_driver' => [
                'class' => AnnotationDriver::class,
                'cache' => 'array',
                'paths' => [
                    __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src'
                    . DIRECTORY_SEPARATOR . 'Entity',
                ]
            ],
            'orm_default'             => [
                'drivers' => [
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                ],
            ],
        ],
    ],
];

The above config would be required per module, due to the usage of the __NAMESPACE__ and __DIR__ global constants.


NOTE: Make sure that namespace names match folder names, e.g.:

module\Address\Entity\Address (last is class name for Address.php)

Folder structure here is:

module\Address\Entity\**.php


Example: Entity class & associated composer.json

From example links:

composer.json

"autoload": {
    "psr-4": {
        "Keet\\Form\\Examples\\": "src/"
    },

Address.php

<?php

namespace Keet\Form\Examples\Entity;

use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Table(name="addresses")
 * @ORM\Entity
 */
class Address extends AbstractEntity
{
...
}

Upvotes: 2

Related Questions