我现在已经存在很多关于这个错误的问题,但所有这些问题都是关于使用YML或Symfony/Zond作为框架的。我正在学习在我的Codeigniter项目中使用Doctrine,并且我已经达到了使用cli工具生成实体的地步:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/entities
瞧,我所有的实体都生成了。尽管我在bootstrap.php中指定了要使用Entity命名空间,但所有生成的类都不使用该命名空间。不管怎样,我只是手动添加的。以下是我在application/models/Entity/Vatpercentage.php中的Entity-Vatpercentage示例:
namespace Entity;
use DoctrineORMMapping as ORM;
/**
* EntityVatpercentage
*
* @ORMTable(name="vatpercentage", uniqueConstraints={@ORMUniqueConstraint(name="id_UNIQUE", columns={"id"}), @ORMUniqueConstraint(name="code_UNIQUE", columns={"code"}), @ORMUniqueConstraint(name="vat_UNIQUE", columns={"vat"})})
* @ORMEntity
*/
class Vatpercentage
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORMColumn(name="vat", type="integer", nullable=false)
*/
private $vat = '0';
/**
* @var string
*
* @ORMColumn(name="code", type="string", length=45, nullable=false)
*/
private $code;
/**
* @var integer
*
* @ORMColumn(name="accountnr", type="integer", nullable=true)
*/
private $accountnr;
}
现在我想调用EntityManager,看看是否可以从数据库中检索到我的一个实体。我的模型中的代码:
public function get_vatpercentages(){
$result = $this->doctrine->em->find('EntityVatpercentage', 1);
但后来我得到了一个例外:
An uncaught Exception was encountered
Type: DoctrineORMMappingMappingException
Message: Class "EntityVatpercentage" 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
我不明白为什么。
为了完整起见,这里还有我在libraris/doctrine.php中的引导程序:
include_once FCPATH . 'vendor/autoload.php';
use DoctrineCommonClassLoader;
use DoctrineORMEntityManager;
use DoctrineORMToolsSetup;
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 EntityUser 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类的方法"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);
现在它工作得很好!