【问题标题】:Doctrine 2 configuration problem, can't seem to find entitiesDoctrine 2 配置问题,似乎找不到实体
【发布时间】:2011-10-17 17:46:39
【问题描述】:

作为一个 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]

我的用户.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)
));

?>

【问题讨论】:

    标签: doctrine-orm


    【解决方案1】:

    这个问题可能是因为你没有加载你的模块。

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

    我希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

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

      Doctrine 2.0 ReflectionException when I try to do YAML Mapping

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

      $ 学说 orm:generate-entities --generate-annotations=1 实体/

      它的作用是生成实体以及所有 映射信息。现在,如果您更改元数据驱动程序配置 您的引导程序使用实体而不是 yaml 文件 架构创建应该可以工作。

      【讨论】:

        【解决方案3】:

        可能,当我遇到同样的麻烦时,原因是没有命名空间。 正如您在生成的实体中看到的那样,没有命名空间,所以 Doctrine 找不到它。

        【讨论】:

        • 应该在哪里添加这个命名空间/缺少的命名空间是什么?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多