本文介绍了Doctrine2将YAML实体与ANNOTATION特性混用不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些实体使用我的 SluggableTrait ,其中包含 name slug 字段。

So I have some entities which uses my SluggableTrait, which contains name and slug fields along it's methods.

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();
    }
}

但是每当我尝试在YAML映射中使用它时实体,数据库不会创建它的字段...

But whenever I try to use it in a YAML mapped entity, database won't create it's fields...

假设我有 Foo

class Foo
{
    use SluggableTrait;

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


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

具有YAML映射

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

更新架构时(甚至创建一个新的空数据库)我的 foo 表将仅具有 id 字段,名称 slug 不存在。

When updating my schema (even creating a new empty database) my foo table will only have id field, name and slug doesn't exists.

如果我在这两个项目,特质和班级。如果混合映射不起作用。

Everything works fine if I use annotation in both items, the Trait and the Class. If mixed mapping, doesn't work.

在特征中使用YAML映射时,只要尝试一下,每当我尝试更新架构时都会收到此错误

When using YAML mapping in my trait, just to give it a try I get this error whenever I try to update my schema

                                                                               [Doctrine\Common\Persistence\Mapping\MappingException]                

Utils\DoctrineBundle\类\Entity\SluggableTrait'不存在

Class 'Utils\DoctrineBundle\Entity\SluggableTrait' does not exist

doctrine:schema:update [--complete] [--dump-sql] [-f | --force] [ --em
[EM]]

doctrine:schema:update [--complete] [--dump-sql] [-f|--force] [--em [EM]]

我试图搜索Trait YAML示例,但没有成功...因此,我现在陷入困境。

I've tried to search for a Trait YAML example without success... So I'm stuck right now.

好吧,经过一些测试,Doctrine不允许您在不重新声明属性的情况下混合使用YAML和ANOTTATION,这显然是我们想要的避免使用特质(并扩展一个类)

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)

我创建了一个PR来关注该

I've created a PR in order to follow thishttps://github.com/doctrine/common/pull/701

此PR允许您使用YAML映射的特征,但不触发某些事件,因此需要更深入的研究。

This PR allows you to use YAML mapped traits, but doesn't trigger some events so it needs to go deeper.

推荐答案

简短答案:

您不能混合使用YAML和注释映射,而Traits不适用于YAML映射。

You can't mix YAML and Annotation mappings and Traits doesn't work with YAML mappings.

这篇关于Doctrine2将YAML实体与ANNOTATION特性混用不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 23:20