简体   繁体   English

Doctrine2将YAML实体与ANNOTATION特性混用不起作用

[英]Doctrine2 mixing YAML entities with ANNOTATION Traits doesn't work

So I have some entities which uses my SluggableTrait , which contains name and slug fields along it's methods. 所以我有一些使用我的SluggableTrait实体,该实体包含方法中的nameslug字段。

trait SluggableTrait
{
    /**
     * @var string
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @var string
     * @ORM\Column(type="string", length=255
     * @Gedmo\Slug(fields={"name"}, updatable=false))
     */
    private $slug;


    /**
     * Set name
     *
     * @param string $name
     *
     * @return SluggableTrait
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set slug
     *
     * @param string $slug
     *
     * @return SluggableTrait
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

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

    /**
     * @return string
     */
    public function __toString()
    {
        return $this->getName() ? $this->getName() : 'n/a';
    }

    public function getClassName()
    {
        $reflect = new \ReflectionClass($this);
        return $reflect->getShortName();
    }
}

But whenever I try to use it in a YAML mapped entity, database won't create it's fields... 但是每当我尝试在YAML映射实体中使用它时,数据库都不会创建它的字段...

Let's say I have Foo class 假设我有Foo

class Foo
{
    use SluggableTrait;

    /**
     * @var integer
     */
    private $id;


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

With the YAML mapping 使用YAML映射

Utils\DoctrineBundle\Entity\Foo:
    type: entity
    id:
        id:
            type: integer
            generator:
                strategy: AUTO

When updating my schema (even creating a new empty database) my foo table will only have id field, name and slug doesn't exists. 当更新我的架构(甚至创建一个新的空数据库)时,我的foo表将仅具有id字段, nameslug不存在。

Everything works fine if I use annotation in both items, the Trait and the Class. 如果我在特质和班级这两个项目中都使用注释,则一切正常。 If mixed mapping, doesn't work. 如果混合映射,则不起作用。

When using YAML mapping in my trait, just to give it a try I get this error whenever I try to update my schema 在特征中使用YAML映射时,只要尝试一下,每当尝试更新架构时都会收到此错误

[2015-12-07 13:20:33] app.ERROR: Doctrine\\Common\\Persistence\\Mapping\\MappingException: Class 'Utils\\DoctrineBundle\\Entity\\SluggableTrait' does not exist (uncaught exception) at /srv/myapp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php line 96 while running console command doctrine:schema:update {"exception":"[object] (Doctrine\\Common\\Persistence\\Mapping\\MappingException(code: 0): Class 'Utils\\DoctrineBundle\\Entity\\SluggableTrait' does not exist at /srv/myapp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96)"} [2015-12-07 13:20:33] app.ERROR:Doctrine \\ Common \\ Persistence \\ Mapping \\ MappingException:在/ srv / myapp / vendor中不存在类'Utils \\ DoctrineBundle \\ Entity \\ SluggableTrait'(未捕获的异常) /doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php第96行,在运行控制台命令doctrine:schema:update {“ exception”:“ [object](Doctrine \\ Common \\ Persistence \\ Mapping \\ MappingException(code :0):类'Utils \\ DoctrineBundle \\ Entity \\ SluggableTrait'在/srv/myapp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96)中不存在

  [Doctrine\\Common\\Persistence\\Mapping\\MappingException] 

Class 'Utils\\DoctrineBundle\\Entity\\SluggableTrait' does not exist 类“ Utils \\ DoctrineBundle \\ Entity \\ SluggableTrait”不存在

doctrine:schema:update [--complete] [--dump-sql] [-f|--force] [--em [EM]] 教义:模式:更新[--complete] [--dump-sql] [-f | --force] [--em [EM]]

I've tried to search for a Trait YAML example without success... So I'm stuck right now. 我一直试图搜索Trait YAML示例,但没有成功...所以我现在陷入困境。

Well, after some tests, Doctrine doesn't allow you to mix YAML and ANOTTATION without redeclaring properties which obviously is what we want to avoid using a Trait (and extending a Class) 好吧,经过一些测试,Doctrine不允许您在不重新声明属性的情况下混合使用YAML和ANOTTATION,这显然是我们要避免使用Trait(并扩展Class)的原因

I've created a PR in order to follow this https://github.com/doctrine/common/pull/701 我创建了一个PR以遵循此https://github.com/doctrine/common/pull/701

This PR allows you to use YAML mapped traits, but doesn't trigger some events so it needs to go deeper. 此PR允许您使用YAML映射特征,但不触发某些事件,因此需要更深入地了解。

Short answer: 简短答案:

You can't mix YAML and Annotation mappings and Traits doesn't work with YAML mappings. 您不能混合使用YAML和Annotation映射,并且Traits不适用于YAML映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM