Doctrine 2.0.4 Configuration Error? [closed]

2020-02-13 04:18发布

问题:

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 8 years ago.

I am using doctrine 2.0.4 .i am not sure where exact wrong here any one can help here?

   <?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();
?>

Resulting in:

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? 

回答1:

It is not a solution but you could try using the commandline tool doctrine.php to generate proxies

php doctrine.php orm:generate-proxies

Maybe you can locate the problem from here



回答2:

I think the line with:

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

should be:

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

Because the first parameter is a namespace of autoloaded classes.