带有 Doctrine 2 的 Codeigniter 3 - Doctrine\ORM\Mapping\MappingException Class 不是有效实体或映射的超级 class
Codeigniter 3 with Doctrine 2 - Doctrine\ORM\Mapping\MappingException Class is not a valid entity or mapped super class
我现在已经有很多关于相同错误的问题,但所有问题都与使用 YML 或 Symfony/Zend 作为框架有关。我正在学习在我的 Codeigniter 项目中使用 Doctrine,并且我已经到了使用 cli 工具生成实体的地步:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/entities
瞧,我的所有实体都已生成。即使我在 bootstrap.php 中指定我想使用 Entity 命名空间,所有生成的 类 也不要使用该命名空间。不管怎样,我只是手动添加的。这是 application/models/Entity/Vatpercentage.php:
中我的实体 Vatpercentage 的示例
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Entity\Vatpercentage
*
* @ORM\Table(name="vatpercentage", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"}), @ORM\UniqueConstraint(name="code_UNIQUE", columns={"code"}), @ORM\UniqueConstraint(name="vat_UNIQUE", columns={"vat"})})
* @ORM\Entity
*/
class Vatpercentage
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="vat", type="integer", nullable=false)
*/
private $vat = '0';
/**
* @var string
*
* @ORM\Column(name="code", type="string", length=45, nullable=false)
*/
private $code;
/**
* @var integer
*
* @ORM\Column(name="accountnr", type="integer", nullable=true)
*/
private $accountnr;
}
现在我想调用 EntityManager 并查看是否可以从我的数据库中检索我的实体之一。我模型中的代码:
public function get_vatpercentages(){
$result = $this->doctrine->em->find('Entity\Vatpercentage', 1);
但是我得到一个异常:
An uncaught Exception was encountered
Type: Doctrine\ORM\Mapping\MappingException
Message: Class "Entity\Vatpercentage" is not a valid entity or mapped super class.
Filename: /Users/pimdietz/Documents/programming/pos/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php
Line Number: 346
我想不通为什么。
为了完整起见,这也是我在 libraries/doctrine 中的引导程序。php:
include_once FCPATH . 'vendor/autoload.php';
use Doctrine\Common\ClassLoader;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
class Doctrine {
public $em;
public function __construct() {
// Load the database configuration from CodeIgniter
require APPPATH . 'config/database.php';
$connection_options = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database'],
'charset' => $db['default']['char_set'],
'driverOptions' => array(
'charset' => $db['default']['char_set'],
),
);
// With this configuration, your model files need to be in application/models/Entity
// e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
$models_namespace = 'Entity';
$models_path = APPPATH . 'models';
$proxies_dir = APPPATH . 'models/proxies';
$metadata_paths = array(APPPATH . 'models/Entity');
//Dev mode disables caching methods, otherwise it will try Apc, memcache, etc.:
$dev_mode = ENVIRONMENT == 'development';
// If you want to use a different metadata driver, change createAnnotationMetadataConfiguration
// to createXMLMetadataConfiguration or createYAMLMetadataConfiguration.
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir);
$this->em = EntityManager::create($connection_options, $config);
$loader = new ClassLoader($models_namespace, $models_path);
$loader->register();
}
}
拜托,拜托拜托,谁能告诉我我做错了什么?
解决了!
我发现我使用的是 SimpleAnnotationReader,它不喜欢我使用的 orm cli 工具命令生成的注释:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/entities
正如您在 Doctrine\ORM\Tools\Setup class 的方法 "createAnnotationMetadataConfiguration" 中看到的,最后一个参数标记了 simpleannotationreader 的使用:
/**
* Creates a configuration with an annotation metadata driver.
*
* @param array $paths
* @param boolean $isDevMode
* @param string $proxyDir
* @param Cache $cache
* @param bool $useSimpleAnnotationReader
*
* @return Configuration
*/
public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
{
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
return $config;
}
所以简而言之,要让它工作,我需要做的就是给它一个使用 simpleannotationreader 的错误标志(在我的 Doctrine.php 引导程序中):
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir, null, false);
现在可以正常使用了!
我现在已经有很多关于相同错误的问题,但所有问题都与使用 YML 或 Symfony/Zend 作为框架有关。我正在学习在我的 Codeigniter 项目中使用 Doctrine,并且我已经到了使用 cli 工具生成实体的地步:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/entities
瞧,我的所有实体都已生成。即使我在 bootstrap.php 中指定我想使用 Entity 命名空间,所有生成的 类 也不要使用该命名空间。不管怎样,我只是手动添加的。这是 application/models/Entity/Vatpercentage.php:
中我的实体 Vatpercentage 的示例
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Entity\Vatpercentage
*
* @ORM\Table(name="vatpercentage", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"}), @ORM\UniqueConstraint(name="code_UNIQUE", columns={"code"}), @ORM\UniqueConstraint(name="vat_UNIQUE", columns={"vat"})})
* @ORM\Entity
*/
class Vatpercentage
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="vat", type="integer", nullable=false)
*/
private $vat = '0';
/**
* @var string
*
* @ORM\Column(name="code", type="string", length=45, nullable=false)
*/
private $code;
/**
* @var integer
*
* @ORM\Column(name="accountnr", type="integer", nullable=true)
*/
private $accountnr;
}
现在我想调用 EntityManager 并查看是否可以从我的数据库中检索我的实体之一。我模型中的代码:
public function get_vatpercentages(){
$result = $this->doctrine->em->find('Entity\Vatpercentage', 1);
但是我得到一个异常:
An uncaught Exception was encountered
Type: Doctrine\ORM\Mapping\MappingException
Message: Class "Entity\Vatpercentage" is not a valid entity or mapped super class.
Filename: /Users/pimdietz/Documents/programming/pos/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php
Line Number: 346
我想不通为什么。
为了完整起见,这也是我在 libraries/doctrine 中的引导程序。php:
include_once FCPATH . 'vendor/autoload.php';
use Doctrine\Common\ClassLoader;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
class Doctrine {
public $em;
public function __construct() {
// Load the database configuration from CodeIgniter
require APPPATH . 'config/database.php';
$connection_options = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database'],
'charset' => $db['default']['char_set'],
'driverOptions' => array(
'charset' => $db['default']['char_set'],
),
);
// With this configuration, your model files need to be in application/models/Entity
// e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
$models_namespace = 'Entity';
$models_path = APPPATH . 'models';
$proxies_dir = APPPATH . 'models/proxies';
$metadata_paths = array(APPPATH . 'models/Entity');
//Dev mode disables caching methods, otherwise it will try Apc, memcache, etc.:
$dev_mode = ENVIRONMENT == 'development';
// If you want to use a different metadata driver, change createAnnotationMetadataConfiguration
// to createXMLMetadataConfiguration or createYAMLMetadataConfiguration.
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir);
$this->em = EntityManager::create($connection_options, $config);
$loader = new ClassLoader($models_namespace, $models_path);
$loader->register();
}
}
拜托,拜托拜托,谁能告诉我我做错了什么?
解决了!
我发现我使用的是 SimpleAnnotationReader,它不喜欢我使用的 orm cli 工具命令生成的注释:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/entities
正如您在 Doctrine\ORM\Tools\Setup class 的方法 "createAnnotationMetadataConfiguration" 中看到的,最后一个参数标记了 simpleannotationreader 的使用:
/**
* Creates a configuration with an annotation metadata driver.
*
* @param array $paths
* @param boolean $isDevMode
* @param string $proxyDir
* @param Cache $cache
* @param bool $useSimpleAnnotationReader
*
* @return Configuration
*/
public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
{
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
return $config;
}
所以简而言之,要让它工作,我需要做的就是给它一个使用 simpleannotationreader 的错误标志(在我的 Doctrine.php 引导程序中):
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir, null, false);
现在可以正常使用了!