【发布时间】:2014-08-06 10:35:20
【问题描述】:
我正在使用带有 codeigniter 的 Doctrine 2。我已经使用作曲家安装和配置了学说。当我试图访问我的存储库时,我得到了一个异常。我正在列出详细信息 -
我的 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 作为 -
<?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;
}
【问题讨论】:
-
您也可以粘贴您的实体文件吗?干杯!
-
感谢@AnjanaSilva,我也添加了实体类。
-
在下面查看我的答案 :)
标签: php codeigniter doctrine-orm doctrine