2011-08-01 84 views
0

作爲一個學說2.1新手我想開始使用學說2,但我堅持什麼似乎基本的東西。但我仍不能環繞它我的頭:學說2的配置問題,似乎無法找到實體

運行

$ doctrine orm:generate-entities Entities 

Processing entity "MyUser" 

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

生成什麼MyUser.php文件我希望(見下文)

我然後繼續生成模式這顯然是因爲類加載器不工作,因爲它似乎沒有找到位於實體目錄下的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; 
    } 
} 

內容/映射/ 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) 
)); 

?> 

回答

0

如果其他人得到了th Ë同樣的問題,從聯線以下後可能會有所幫助:

Doctrine 2.0 ReflectionException when I try to do YAML Mapping

我遇到同樣的麻煩。訣竅是產生從 YAML文件實體與--generate的註解選項設置爲1。我已經 抄我下面使用的命令:

$學說的ORM:生成實體--generate的註解= 1實體/

這樣做的是它會生成實體以及所有 映射信息。現在,如果您更改 中的元數據驅動程序配置,則您的引導將使用實體而不是yaml文件, 架構創建應該可以工作。

0

也許,當我遇到同樣的問題時,原因是沒有命名空間。 正如你可以在你生成的實體中看到的那樣沒有命名空間,所以Doctrine找不到它。

+0

應該在哪裏添加這個命名空間/什麼是缺少的命名空間? – Shawn

1

此問題可能是由於您未加載模塊而導致的。

如果您使用ZF2,請確保您將模塊添加到application.config.php中。 如果你使用ZF3,你應該將它包含在modules.config.php

我希望這可以幫助某人。