Doctrine 2.0.4配置错误?

时间:2011-05-04 04:51:04

标签: php doctrine doctrine-orm

我正在使用doctrine 2.0.4。我不确定这里有什么问题可以帮助吗?

   <?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Configuration,
    Doctrine\ORM\EntityManager,
    Doctrine\ORM\Tools\EntityGenerator,
    Doctrine\Common\Cache\ApcCache,
    Entities\User,Entity\Address;

$RootPath = $_SERVER['DOCUMENT_ROOT'] . '/';
require $RootPath.'doctrine2/Doctrine/Common/ClassLoader.php';

$lib = $RootPath.'doctrine2/';
$lib1 = $RootPath.'MyProject/';
$classLoader = new ClassLoader('Doctrine',$lib);
$classLoader->register();

$classLoader = new ClassLoader('Entities',$lib1);
$classLoader->register();

$classLoader = new ClassLoader('Proxies',$lib1);
$classLoader->register();

$config = new Configuration;
$cache= new ApcCache;

$driverImpl = $config->newDefaultAnnotationDriver($lib1.'Entities');
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);


$config->setProxyDir($lib1.'Proxies');
$config->setProxyNamespace('MyProject\Proxies');


 $config->setAutoGenerateProxyClasses(true);


$connectionOptions = array(
    'driver' => 'pdo_mysql',
    'dbname' => 'test',
    'user' => 'abc',
    'password' => '123321',
    'host' => '127.0.0.1');



$em = EntityManager::create($connectionOptions, $config);
echo "<pre>";
print_r($em);
// custom datatypes (not mapped for reverse engineering)
/*$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
*/

// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
    $em->getConnection()->getSchemaManager()
);
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em);
$cmf->setEntityManager($em); 
$classes = $driver->getAllClassNames();
$metadata = $cmf->getAllMetadata(); 
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, $lib1 . 'Entities');
echo  'Done';
$q = $em->createQuery("select u from MyProject\Entities\Dept u ");
$users = $q->getResult();
?>

导致:

Error::Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message '[Semantical Error] line 0, col 14 near 'MyProject\Entities\Dept': Error: Class 'MyProject\Entities\Dept' is not defined.

Dept.php in Entities code

<?php


/**
 * Dept
 *
 * @Table(name="dept")
 * @Entity
 */
class Dept
{
    /**
     * @var integer $deptno
     *
     * @Column(name="deptno", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */

    private $deptno;

    /**
     * @var string $dname
     *
     * @Column(name="dname", type="string", length=50, nullable=false)
     */
    private $dname;

    /**
     * @var string $location
     *
     * @Column(name="location", type="string", length=50, nullable=false)
     */
    private $location;


    /**
     * Get deptno
     *
     * @return integer $deptno
     */
    public function getDeptno()
    {
        return $this->deptno;
    }

    /**
     * Set dname
     *
     * @param string $dname
     */
    public function setDname($dname)
    {
        $this->dname = $dname;
    }

    /**
     * Get dname
     *
     * @return string $dname
     */
    public function getDname()
    {
        return $this->dname;
    }

    /**
     * Set location
     *
     * @param string $location
     */
    public function setLocation($location)
    {
        $this->location = $location;
    }

    /**
     * Get location
     *
     * @return string $location
     */
    public function getLocation()
    {
        return $this->location;
    }
}
    and Proxies class not generated here but Entities is generated...where exact wrong here? 

2 个答案:

答案 0 :(得分:1)

这不是解决方案,但您可以尝试使用命令行工具doctrine.php来生成代理

  

php doctrine.php orm:generate-proxies

也许你可以从这里找到问题

答案 1 :(得分:0)

我认为该行:

$classLoader = new ClassLoader('Entities',$lib1);

应该是:

$classLoader = new ClassLoader('MyProject\Entities',$lib1);

因为第一个参数是自动加载类的命名空间。