2012-08-10 161 views
6

我正在使用Doctrine2和Symfony2驗證器組件的CodeIgniter項目。導入註釋時遇到問題

我所有的Doctrine2實體use Doctrine\ORM\Mapping和實體管理器都能識別它們。我的實體註釋看起來像這樣:

/** 
* @Entity(repositoryClass = "UserRepository") 
* @Table(name = "user") 
* @HasLifecycleCallbacks() 
*/ 

在這一點上,我能夠堅持實​​體沒有任何麻煩。當我嘗試使用Symfony2 Validator組件時,第一個問題就出現了。當我嘗試驗證User對象,我得到這個錯誤:

[Semantical Error] The annotation "@Entity" in class Entity\User was never imported. Did you maybe forget to add a "use" statement for this annotation?

唯一的「修復」這個問題是通過use Doctrine\Mapping\Entity,但我必須這樣做,對於正在使用我的實體每註釋(表,列,ManyToOne等)。我試圖弄清楚爲什麼我需要明確地註釋use每個註釋類,而不是自動識別它們(不應該use Doctrine\ORM\Mapping授予我訪問該名稱空間內所有類的權限?)。

所以,我然後試着use Doctrine\ORM\Mapping as ORMORM\預填所有我的註釋。例如:@ORM\Entity()。 Symfony2驗證器停止抱怨,但現在Doctrine2抱怨Class Entity\User is not a valid entity or mapped super class.我不知道爲什麼這種方法適用於Validator,而不是Doctrine2。如果我運行控制檯命令doctrine:orm:info,我的User實體無法識別。

因爲這是一個CodeIgniter應用程序,所以我自動加載Symfony2和Doctrine2庫。我的自動加載代碼如下:

# Symfony2 ClassLoader component 
require_once __DIR__ . '/application/libraries/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php'; 
$loader = new \Symfony\Component\ClassLoader\UniversalClassLoader(); 
$loader->register(); 
$loader->registerNamespace('Symfony', __DIR__ . '/application/libraries/symfony/src'); 
$loader->registerNamespace('Doctrine', __DIR__ . '/application/libraries/doctrine/lib'); 

# Doctrine2 
require_once __DIR__ . '/application/libraries/doctrine/lib/Doctrine/Common/Annotations/AnnotationRegistry.php'; 
use Doctrine\Common\Annotations\AnnotationRegistry; 
AnnotationRegistry::registerLoader(function($class) use ($loader) { 
    $loader->loadClass($class); 
    $classExists = class_exists($class, false); 
    return $classExists; 
}); 
AnnotationRegistry::registerFile(__DIR__ . '/application/libraries/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); 

如果我有ORM\或不前言一切,我不在乎,我只想找到一種解決方案,該Symfony2的驗證和Doctrine2將兩者與工作。有任何想法嗎?

回答

19

當使用Silex,Doctrine2和Symfony-Validator時,我遇到了類似的問題。

解決方案是爲了避免使用SimpleAnnotationsReader,而是使用普通的AnnotationReader(看看Doctrine\ORM\Configuration::newDefaultAnnotationDriver)。然後,您可以用ORM\(當然,在每個實體中插入use Doctrine\ORM\Mapping as ORM;)爲每個實體開頭。

+0

那麼它沒有'ORM \'工作的情況下,所以我們編碼每個實體的方式,現在突然停止工作,沒有原因,我們留下了維護噩夢? – Brandon 2017-01-23 19:41:02

3

爲 添加use聲明您在entities中使用的每個註釋。例如:

use Doctrine\ORM\Mapping\Entity; 
use Doctrine\ORM\Mapping\Table; 
use Doctrine\ORM\Mapping\Id; 
use Doctrine\ORM\Mapping\Column; 
use Doctrine\ORM\Mapping\GeneratedValue; 

但是,您可能不會使用這種方法有趣。您可以將所有use語句寫入一個文件,並將其包含在您的entities中。

+3

'使用Doctrine \ ORM \ Mapping作爲ORM;''讓你使用'@ORM \ Entity'等等,它更加簡潔。 user1825294是正確的。 – konrad 2015-03-01 15:37:11

+0

是的你是對的,thanx – 2015-03-23 18:03:08

+0

這不是你想要的解決方案!你也不會有註釋。 – 2017-12-11 10:01:52

6

我加入解決了這個問題,下面我的實體文件:

use Doctrine\ORM\Mapping as ORM; 

所以我的文件現在看起來像:

<?php 

namespace Acme\CustomBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Example 
* 
* @ORM\Entity 
* @ORM\Table(name="example") 
* 
*/ 
class Example 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string", length=125) 
    * 
    * @var string 
    */ 
    private $title; 

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

    /** 
    * Set title 
    * 
    * @param string $title 
    * 
    * @return Example 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

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

} 
+0

這並不適用於特別需要id註釋。當@ Mostafa的回答顯示時,我必須單獨指定它。 – Jestep 2017-10-26 19:33:01

0

如果要測試你的測試失敗,因爲這一點。確保在phpunit設置中配置了正確的自動加載。

對於一個非常具體的情況,我的問題是升級我的symfony版本,而之前我有bootstrap.cache.php文件,但在新版本上,它是app/autoload。我不得不改變我的phpunit.xml配置文件,並將我的引導選項設置爲該文件bootstrap="autoload.php"