2017-05-28 26 views
1

當我嘗試驗證我的symfony的架構,我得到的錯誤:如何在用戶定義的symfony Bundle中配置Doctrine Entity Annotation自動加載?

[Doctrine\Common\Annotations\AnnotationException]        
[Semantical Error] The annotation "@Doctrine\Orm\Mapping\Entity" in class C 
hill\EmailUser\Entity\EmailUser does not exist, or could not be auto-loaded 

我看到,在這個問題:Trouble with importing annotations,它是說,我們需要從學說簡單的註解讀者改變。儘管這似乎有些奇怪,使束配置在自動裝,我想這樣做,通過應用程序/ autoloader.php:

use Doctrine\ORM\Configuration; 

path = __DIR__."/../vendor/path/to/annotationDriver.php" 
Configuration::newDefaultAnnotationDriver(path, false); 

我還試圖研究,看看是否能找到像一個參數:

doctrine.orm.mappings.annotations.simple_annotations_reader 

,我可以設置爲false,但我找不到任何東西。

正如你在下面看到的,我也嘗試了爲我的包添加映射到config.yml中的ORM設置。

composer.json:

{ 
    "name": "root/photoproject", 
"license": "proprietary", 
"type": "project", 
"autoload": { 
    "psr-4": { 
     "": "src/" 
    }, 
    "classmap": [ 
     "app/AppKernel.php", 
     "app/AppCache.php" 
    ] 
}, 
"autoload-dev": { 
    "files": [ 
     "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php" 
    ] 
}, 
"require": { 
    "php": ">=5.3.9", 
    "doctrine/annotations": "^1.4", 
    "doctrine/doctrine-bundle": "~1.4", 
    "doctrine/migrations": "^1.5", 
    "doctrine/orm": "^2.5", 
    "incenteev/composer-parameter-handler": "~2.0", 
    "sensio/distribution-bundle": "~4.0", 
    "sensio/framework-extra-bundle": "^3.0.2", 
    "symfony/monolog-bundle": "^3.0.2", 
    "symfony/swiftmailer-bundle": "~2.3,>=2.3.10", 
    "symfony/symfony": "2.8.*", 
    "twig/twig": "^1.0||^2.0" 
}, 
"require-dev": { 
    "sensio/generator-bundle": "~3.0", 
    "symfony/phpunit-bridge": "~2.7" 
}, 
"scripts": { 
    "symfony-scripts": [ 
     "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile", 
     "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget" 
    ], 
    "post-install-cmd": [ 
     "@symfony-scripts" 
    ], 
    "post-update-cmd": [ 
     "@symfony-scripts" 
    ] 
}, 
"config": { 
    "bin-dir": "bin", 
    "sort-packages": true 
}, 
"extra": { 
    "symfony-app-dir": "app", 
    "symfony-web-dir": "web", 
    "symfony-assets-install": "relative", 
    "incenteev-parameters": { 
     "file": "app/config/parameters.yml" 
    }, 
    "branch-alias": null 
} 
} 

應用程序/配置/ config.yml

imports: 
- { resource: parameters.yml } 
- { resource: security.yml } 
- { resource: services.yml } 

# Put parameters here that don't need to change on each machine where the app is deployed 
# http://symfony.com/doc/current/best_practices /configuration.html#application-related-configuration 
parameters: 
locale: en 

    framework: 
#esi: ~ 
#translator: { fallbacks: ['%locale%'] } 
secret: '%secret%' 
router: 
    resource: '%kernel.root_dir%/config/routing.yml' 
    strict_requirements: ~ 
form: ~ 
csrf_protection: ~ 
validation: { enable_annotations: true } 
serializer: { enable_annotations: true } 
templating: 
    engines: ['twig'] 
default_locale: '%locale%' 
trusted_hosts: ~ 
trusted_proxies: ~ 
session: 
    # handler_id set to null will use default session handler from php.ini 
    handler_id: ~ 
fragments: ~ 
http_method_override: true 

    # Twig Configuration 
    twig: 
debug: '%kernel.debug%' 
strict_variables: '%kernel.debug%' 
    # Doctrine Configuration 
    doctrine: 
dbal: 
    driver: pdo_mysql 
    host: '%database_host%' 
    port: '%database_port%' 
    dbname: '%database_name%' 
    user: '%database_user%' 
    password: '%database_password%' 
    charset: UTF8 
    # if using pdo_sqlite as your database driver: 
    # 1. add the path in parameters.yml 
    #  e.g. database_path: '%kernel.root_dir%/data/data.db3' 
    # 2. Uncomment database_path in parameters.yml.dist 
    # 3. Uncomment next line: 
    #path: '%database_path%' 

orm: 
    auto_generate_proxy_classes: '%kernel.debug%' 
    entity_managers: 
     default: 
      naming_strategy: doctrine.orm.naming_strategy.underscore 
      auto_mapping: true 
      mappings: 
       ChillEmailUserBundle: ~ 

    # Swiftmailer Configuration 
    swiftmailer: 
transport: '%mailer_transport%' 
hostity/l: { type: memory } 

的src /冷卻/ EmailUserBundle /實體/ EmailUser.php

namespace Chill\EmailUser\Entity; 

use Doctrine\Orm\Mapping as ORM; 
use Symfony\Component\Validator\Contraints as Assert; 

/** 
* @ORM\Entity 
*/ 

class EmailUser 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string", length="1023") 
    * @Assert\Email(message="Not a valid email") 
    */ 
    private $email; 

    /** 
    * @ORM\Column(type="string", length="4096") 
    */ 
    private $password; 

    /** 
    * @ORM\Column(type="string", length="4096") 
    */ 
    private $salt; 

    /** 
    * @ORM\ManyToMany(targetEntity="Role") 
    */ 
    private $roles; 
} 

所以我的問題我該如何做到Symfony公認的答案是建議的(儘管最好讓這個軟件包相對可重用)。

感謝


<?php 

use Doctrine\Common\Annotations\AnnotationRegistry; 
use Composer\Autoload\ClassLoader; 

/** @var ClassLoader $loader */ 
$loader = require __DIR__.'/../vendor/autoload.php'; 
AnnotationRegistry::registerLoader(array($loader, 'loadClass')); 
return $loader;        

回答

4

AFAIK你必須爲AnnotationRegistry寄存器加載。

更詳細的文檔是available here。在symfony項目的情況下,最簡單的方法是使用app/autoload具有下列內容:

<?php 

use Composer\Autoload\ClassLoader; 
use Doctrine\Common\Annotations\AnnotationRegistry; 

/** 
* @var ClassLoader $loader 
*/ 
$loader = require __DIR__.'/../vendor/autoload.php'; 

AnnotationRegistry::registerLoader(array($loader, 'loadClass')); 

return $loader; 

,因爲需要的,通常捆綁使用XML配置的容器,路由器等。

如果您更喜歡註釋,那就是您的私有捆綁軟件只需註冊載入器,如上所示。如果你想公開,我建議xmls的配置。

+0

欣賞快速回復。我更新了我的應用程序/ autoload.php並加入了這個問題,但我仍然看到錯誤。我想我可能只需要切換到XML。 –

+0

這就是爲什麼教義註解通過全局註冊表使用自己的自動加載機制。如果您想知道註釋註冊表是全局的,那麼沒有其他方法可以直接解決自動加載註釋類的體系結構問題。此外,如果你考慮PHP自動加載,那麼你認識到它是一個全球性的。「 所以我認爲你的回答是正確的,」不要「,我只需要使用XML –

+0

你確定你的'bin/console'使用'app/autoload'?還有一些隨意的想法:用Doctrine \ ORM \ Mapping'替換'Doctrine \ Orm \ Mapping'(區分大小寫),清除緩存(可能您的控制檯使用'prod' env並緩存註釋)。你如何驗證模式? –

相關問題