Symfony2注释" @Gedmo \ Mapping \ Annotation \ Timestampable"不存在,或无法自动加载

时间:2016-07-29 08:53:38

标签: php mongodb symfony doctrine-orm

似乎注释或命名空间的路径是错误的,但我无法弄清楚原因。 php bin/console doctrine:mongodb:mapping:info输出

[FAIL] AppBundle\Document\Test\Test
[Semantical Error] The annotation "@Gedmo\Mapping\Annotation\Timestampable" in property AppBundle\Document\Test\Test::$createdAt does not exist, or could not be auto-loaded.

实体档案:

<?php

namespace AppBundle\Document\Test;

use AppBundle\Document\Question\Question;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @MongoDB\Document(repositoryClass="AppBundle\Document\Test\TestRepository")
 * @MongoDB\HasLifecycleCallbacks()
 */
class Test
{
    /**
     * @MongoDB\Id(strategy="auto")
     */
    protected $id;


    /**
     * @MongoDB\Field(type="string")
     */
    protected $lastName;


    /**
     * @Gedmo\Timestampable(on="create")
     * @MongoDB\Timestamp()
     */
    protected $createdAt;

Stackoverflow已警告我,我的帖子中主要有代码,但我想我必须显示composer.json部分:

"require": {
    "alcaeus/mongo-php-adapter": "^1.0",
    "symfony/symfony": "3.*",
    "doctrine/orm": "^2.5",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/common": "~2.4",        
    "doctrine/doctrine-cache-bundle": "^1.2",
    "doctrine/mongodb-odm": "^1.0",
    "doctrine/mongodb-odm-bundle": "3.*",
    "gedmo/doctrine-extensions": "^2.4",
    "symfony/monolog-bundle": "^2.8",
    "sensio/distribution-bundle": "^5.0",
    "sensio/framework-extra-bundle": "^3.0.2",
    "incenteev/composer-parameter-handler": "^2.0",
    "friendsofsymfony/user-bundle": "dev-master",
    "stof/doctrine-extensions-bundle": "^1.2",
},

是的,我在autoload.php中有AnnotationDriver::registerAnnotationClasses();。我看过vendor/composer/autoload_namespaces.php并且看起来似乎还可以:

<?php

// autoload_namespaces.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
[...]
    'Gedmo\\' => array($vendorDir . '/gedmo/doctrine-extensions/lib'),
    'Doctrine\\ORM\\' => array($vendorDir . '/doctrine/orm/lib'),
    'Doctrine\\ODM\\MongoDB' => array($vendorDir . '/doctrine/mongodb-odm/lib'),
    'Doctrine\\MongoDB' => array($vendorDir . '/doctrine/mongodb/lib'),
    'Doctrine\\DBAL\\' => array($vendorDir . '/doctrine/dbal/lib'),
    'Doctrine\\Common\\Lexer\\' => array($vendorDir . '/doctrine/lexer/lib'),
    'Doctrine\\Common\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib'),
    'Doctrine\\Common\\Collections\\' => array($vendorDir . '/doctrine/collections/lib'),
    'Doctrine\\Common\\Annotations\\' => array($vendorDir . '/doctrine/annotations/lib'),
    'Behat\\Transliterator' => array($vendorDir . '/behat/transliterator/src'),
);

2 个答案:

答案 0 :(得分:2)

根据documentation:您需要在app/config.yml中激活可翻译:

stof_doctrine_extensions:
    mongodb:
        default:
            translatable: true

并将翻译实体映射添加到doctrine:

doctrine:
    orm:
        mappings:
            gedmo_translatable:
                type: annotation
                prefix: Gedmo\Translatable\Document
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Document"
                alias: GedmoTranslatable # (optional) it will default to the name set for the mapping
                is_bundle: false
            gedmo_translator:
                type: annotation
                prefix: Gedmo\Translator\Document
                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translator/Document"
                alias: GedmoTranslator # (optional) it will default to the name set for the mapping
                is_bundle: false

答案 1 :(得分:0)

找到有效的解决方案。必须在autoload.php中手动将注释文件添加到注册表中:

AnnotationRegistry::registerFile(__DIR__ . '/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Mapping/Annotation/Timestampable.php');