我想在我的項目中使用Doctrine 2。我遇到了一些問題。我閱讀文檔,但可能我做錯了什麼。學說,名稱空間和自動載入實體
我想自動加載實體類。而從文檔方法是行不通的。
我的bootstrap.php
<?php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver;
$paths = array("../Entities");
$isDevMode = false;
$classLoader = new \Doctrine\Common\ClassLoader('Entities','../Entities');
$classLoader->register();
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'xxx',
'password' => 'xxx',
'dbname' => 'xxx',
);
$driver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(new Doctrine\Common\Annotations\AnnotationReader(),array('../Entities'));
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$config->setMetadataDriverImpl($driver);
$em = EntityManager::create($dbParams, $config);
我有我的模型類的實體目錄。我已經使用教義客戶端生成了它們。他們看起來不錯。開頭有ids,setters,getters和namespace。
/Entities/ArticleCat.php
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* ArticleCat
*
* @ORM\Table(name="article_cat")
* @ORM\Entity
*/
class ArticleCat
{
我的劇本,我想用學說:
<?php
require_once 'bootstrap.php';
$article = $em->find('ArticleCat', 21);
echo $article->getName();
它不工作。它僅在我以這種方式使用時纔有效,並且我從實體模型中刪除名稱空間。
<?php
require_once 'bootstrap.php';
require_once 'Entities/ArticleCat.php'; //this line added (manual load)
$article = $em->find('ArticleCat', 21);
echo $article->getName();
什麼是使用原則和自動加載實體的正確方法?爲什麼命名空間是一個問題?
我的錯誤:
Fatal error: Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException'
with message 'Class 'ArticleCat' does not exist' in
/myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96
Stack trace: #0 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php(40):
Doctrine\Common\Persistence\Mapping\MappingException::nonExistingClass('ArticleCat')
#1 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping
/AbstractClassMetadataFactory.php(267): Doctrine\Common\Persistence\Mapping
\RuntimeReflectionService->getParentClasses('ArticleCat')
#2 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping
/AbstractClassMetadataFactory.php(297): Doctrine\Common\Persistence\Mapping
\AbstractClassMetadataFactory->getParentClasses('ArticleCat')
#3 /myproject/vendor/doc in /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence
/Mapping/MappingException.php on line 96
http://stackoverflow.com/q/20348484/637210這可能是有用的,你可以添加命名空間別名和使用它像 「找到( 'NS:ArticleCat',21)」 – Mikl