如何在用户定义的symfony捆绑包中配置教义实体注释自动加载? [英] How to configure Doctrine Entity Annotation autoloading in User-defined symfony Bundle?

查看:61
本文介绍了如何在用户定义的symfony捆绑包中配置教义实体注释自动加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试验证我的symfony模式时,出现错误:

When I try to validate my symfony schema, I get the error:

[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 

我在以下问题中看到:无法导入注释,据说我们需要从 Doctrine简单注释阅读器中进行更改。即使在自动加载器中进行捆绑配置似乎很奇怪,我还是尝试通过app / autoloader.php来做到这一点:

I see that in this question: Trouble with importing annotations, it is said we need to change from Doctrine Simple Annotation Reader. Even though it seems strange to make bundle config in the autoloader, I tried doing that via app/autoloader.php:

use Doctrine\ORM\Configuration;

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

我还尝试研究是否可以找到像这样的参数:

I also tried researching to see if I could find a parameter like:

doctrine.orm.mappings.annotations.simple_annotations_reader

我可以将其设置为false,但找不到任何内容。

that I could set to false, but I couldn't find anything.

正如您将在下面看到的,我还尝试了手动为我的映射添加映射捆绑到config.yml中的ORM设置。

As you'll see below, I also tried manualling adding mapping for my bundle to the ORM setting in config.yml.

composer.json:

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
}
}

app / config / config.yml

app/config/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 / Chill / EmailUserBundle / Entity / EmailUser .php

src/Chill/EmailUserBundle/Entity/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中所接受的答案(同时最好使捆绑包相对可重用)。

So my question is how can I do what the accepted answer was suggesting in Symfony (while preferably keeping the bundle relatively re-usable).

谢谢

<?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;                            


推荐答案

AFAIK,您必须为<$ c $注册装载程序c> AnnotationRegistry 。

AFAIK you have to register loader for AnnotationRegistry.

更详细的文档是在此处可用。对于symfony项目,最简单的方法是使用具有以下内容的 app / autoload

More detailed documentation is available here. In case of symfony project the easiest way is to use app/autoload with following contents:

<?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配置容器,路由器等。

Because of that need, bundles usually configure container, router, etc. using XML.

如果您喜欢注释,而这是您的私人捆绑包,请像上面显示的那样注册加载程序。如果要公开,建议使用xml进行配置。

If you prefer annotations and that's your private bundle just register loader as I shown above. If you want to make it public, I suggest xmls for config.

这篇关于如何在用户定义的symfony捆绑包中配置教义实体注释自动加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆