Zend 框架 3 - 未找到 Doctrine 实体
Zend framework 3 - Doctrine entity not found
目的是创建一个模块,在其中插入所有实体,然后在其他模块中使用它们。这就是为什么我创建了一个名为 Entity 的模块,我在其中输入了实体。不幸的是,当我尝试在基本控制器中使用这些实体之一时,出现错误:class not found
这是我的作曲家
"autoload": {
"psr-4": {
"Application\": "module/Application/src/",
"Entity\": "module/Entity/src/"
}
},
我已经执行了这个命令
composer dump-autoload
我的实体 class 在
下
module/Entity/src/Model/
这是我的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;
}
}
这是我的 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'
]
]
],
],
在控制器操作中我尝试了这个
$entity = new SysUserRole();
错误是
Class 'Entity\SysUserRole' not found
编辑:
错误已更改
我将此代码移入 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'
]
]
],
]
新错误是
The class 'Entity\Model\SysUserRole' was not found in the chain configured namespaces \Entity\
文件夹结构和 psr-4 注册命名空间。我希望实体名称空间是 Entity\Model
,然后是 class 名称。
顺便说一句,出于某种目的使用模块可能是更好的方法。为了模块的目的(例如,用户模块中的用户和配置文件实体对象),每个模块都有实体是有意义的。
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'
],
],
],
],
];
由于使用了 __NAMESPACE__
和 __DIR__
全局常量,每个模块都需要上述配置。
注意:确保命名空间名称与文件夹名称匹配,例如:
module\Address\Entity\Address
(last is class name for Address.php
)
这里的文件夹结构是:
module\Address\Entity\**.php
示例:Entity class & associated composer.json
来自示例链接:
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
{
...
}
目的是创建一个模块,在其中插入所有实体,然后在其他模块中使用它们。这就是为什么我创建了一个名为 Entity 的模块,我在其中输入了实体。不幸的是,当我尝试在基本控制器中使用这些实体之一时,出现错误:class not found
这是我的作曲家
"autoload": {
"psr-4": {
"Application\": "module/Application/src/",
"Entity\": "module/Entity/src/"
}
},
我已经执行了这个命令
composer dump-autoload
我的实体 class 在
下module/Entity/src/Model/
这是我的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;
}
}
这是我的 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'
]
]
],
],
在控制器操作中我尝试了这个
$entity = new SysUserRole();
错误是
Class 'Entity\SysUserRole' not found
编辑:
错误已更改
我将此代码移入 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'
]
]
],
]
新错误是
The class 'Entity\Model\SysUserRole' was not found in the chain configured namespaces \Entity\
文件夹结构和 psr-4 注册命名空间。我希望实体名称空间是 Entity\Model
,然后是 class 名称。
顺便说一句,出于某种目的使用模块可能是更好的方法。为了模块的目的(例如,用户模块中的用户和配置文件实体对象),每个模块都有实体是有意义的。
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'
],
],
],
],
];
由于使用了 __NAMESPACE__
和 __DIR__
全局常量,每个模块都需要上述配置。
注意:确保命名空间名称与文件夹名称匹配,例如:
module\Address\Entity\Address
(last is class name forAddress.php
)
这里的文件夹结构是:
module\Address\Entity\**.php
示例:Entity class & associated composer.json
来自示例链接:
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
{
...
}