我正在使用带有codeigniter的Doctrine 2。我已经使用composer安装和配置了doctrine。当我想访问我的存储库时,我得到了一个异常。我正在列出细节 -
我的composer.json文件是 -
{
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"" : "application",
"Myapp\\Entity\\": "application/models/entities/",
"Myapp\\Repository\\": "application/models/repositories/"
}
},
"require": {
"doctrine/common": "2.4.*",
"doctrine/dbal": "2.4.*",
"doctrine/orm": "2.4.*"
}
}
我创建了application / libraries / doctrine.php as -
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\EntityManager;
class Doctrine
{
private $em;
public function __construct()
{
$app_path = getcwd()."/application/";
// 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' => 'mydb',
'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
$entity_path = $app_path. 'models/entities';
// Set $dev_mode to TRUE to disable caching while you develop
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array($entity_path), $isDevMode);
$this->em = EntityManager::create($connection_options, $config);
}
public function getEntityManager() {
return $this->em;
}
}
我的控制器代码是 -
class Home extends CI_Controller{
function __construct()
{
global $page;
parent::__construct();
}
function index()
{
global $page;
$x = $this->doctrine->getEntityManager()->getRepository('Myapp\\Entity\\MyEntity');
var_dump($x);
die;
}
}
我收到以下错误 -
Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class "Myapp\Entity\MyEntity" is not a valid entity or mapped super class.' in /home/piyusht/Projects/XXX/YYY/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php on line 336
我的实体课程如下 -
<?php
namespace Myapp\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* MyEntity
*
*
*
* @ORM\Entity(repositoryClass="Myapp\\Repository\\MyEntityRepository")
*/
class MyEntity
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
}
答案 0 :(得分:0)
尝试将此行更改为
$config = Setup::createAnnotationMetadataConfiguration(array($entity_path), $isDevMode);
此
$config = Setup::createAnnotationMetadataConfiguration(array($entity_path), $isDevMode, null, null, false);
很可能它使用SimpleAnnotationReader,因此您需要使用AnnotationReader将名称空间前缀用作ORM \。
希望这有帮助。
干杯!
答案 1 :(得分:0)
我在我的库/ doctrine.php中添加了以下内容并且它有效 -
$driver = $config->newDefaultAnnotationDriver(
APPPATH . '/models/entities', false
);