Doctrine 2配置问题,似乎找不到实体

时间:2011-08-01 20:02:12

标签: doctrine-orm

作为一个Doctrine 2.1新手我试图开始使用Doctrine 2,但我仍然坚持看似基本的东西。但我仍然无法绕过它:

运行

$ doctrine orm:generate-entities Entities

Processing entity "MyUser"

Entity classes generated to "/home/lucvh/NetBeansProjects/doctrinetest/Entities"

生成我期望的MyUser.php文件(见下文)

然后我继续生成显然失败的模式,因为类加载器不工作,因为它似乎没有找到位于Entities目录下的MyUser类。我错过了什么......

$ doctrine orm:schema-tool:create
PHP Warning:  class_parents(): Class MyUser does not exist and could not be loaded in /usr/share/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 223
PHP Warning:  array_reverse() expects parameter 1 to be array, boolean given in /usr/share/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 223
PHP Warning:  Invalid argument supplied for foreach() in /usr/share/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 223



  [ReflectionException]        
  Class MyUser does not exist  



orm:schema-tool:create [--dump-sql]

MyUser.php:

<?php



use Doctrine\ORM\Mapping as ORM;

/**
 * MyUser
 */
class MyUser
{
    /**
     * @var string $Firstname
     */
    private $Firstname;

    /**
     * @var string $Lastname
     */
    private $Lastname;

    /**
     * @var string $Email
     */
    private $Email;

    /**
     * @var boolean $Enabled
     */
    private $Enabled;

    /**
     * @var integer $Id
     */
    private $Id;


    /**
     * Set Firstname
     *
     * @param string $firstname
     */
    public function setFirstname($firstname)
    {
        $this->Firstname = $firstname;
    }

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

    /**
     * Set Lastname
     *
     * @param string $lastname
     */
    public function setLastname($lastname)
    {
        $this->Lastname = $lastname;
    }

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

    /**
     * Set Email
     *
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->Email = $email;
    }

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

    /**
     * Set Enabled
     *
     * @param boolean $enabled
     */
    public function setEnabled($enabled)
    {
        $this->Enabled = $enabled;
    }

    /**
     * Get Enabled
     *
     * @return boolean 
     */
    public function getEnabled()
    {
        return $this->Enabled;
    }

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

config / mappings / xml / MyUser.dcm.xml的内容

<?xml version="1.0"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="MyUser">
    <id name="Id" type="integer">
      <generator strategy="AUTO"/>
    </id>
    <field name="Firstname" type="string" nullable="true"/>
    <field name="Lastname" type="string" nullable="true"/>
    <field name="Email" type="string" nullable="true"/>
    <field name="Enabled" type="boolean" nullable="true"/>
  </entity>
</doctrine-mapping>

cli-config.php的内容

<?php
require_once '/usr/share/php/Doctrine/Common/ClassLoader.php';


$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();

$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();

$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');


$driverImpl = new Doctrine\ORM\Mapping\Driver\XmlDriver(__DIR__."/config/mappings/xml");
$config->setMetadataDriverImpl($driverImpl);


$connectionOptions = array(
    'driver' => 'pdo_pgsql',
    'host' => 'localhost',
    'user' => 'xxx',
    'password' => 'xxx',
    'dbname' => 'xx'
);

$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
    'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));

?>

3 个答案:

答案 0 :(得分:1)

可能是因为您没有加载模块而导致此问题。

如果您使用ZF2,请确保在application.config.php中添加模块。 如果您使用ZF3,则应将其包含在modules.config.php

我希望这有助于某人。

答案 1 :(得分:0)

如果其他人遇到同样的问题,链接线程中的以下帖子可能会有所帮助:

Doctrine 2.0 ReflectionException when I try to do YAML Mapping

  

我遇到了同样的麻烦。诀窍是从中生成实体   将--generate-annotations选项设置为1的yaml文件。我已经   复制了我在下面使用的命令:

     

$ doctrine orm:generate-entities --generate-annotations = 1 Entities /

     

这样做是因为它与所有实体一起生成实体   映射信息。现在,如果您更改元数据驱动程序配置   你的bootstrap使用实体而不是yaml文件   架构创建应该有效。

答案 2 :(得分:0)

也许,当我遇到同样的麻烦时,原因是没有命名空间。 正如您在生成的Entity中看到的那样,没有命名空间,因此Doctrine无法找到它。