【问题标题】:Doctrine2 mixing YAML entities with ANNOTATION Traits doesn't workDoctrine2 将 YAML 实体与 ANNOTATION Traits 混合不起作用
【发布时间】:2015-12-07 11:57:14
【问题描述】:

所以我有一些实体使用我的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();
    }
}

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

假设我有 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 字段,nameslug 不存在。

如果我在 Trait 和 Class 这两个项目中使用注释,一切都会正常工作。如果是混合映射,则不起作用。

在我的 trait 中使用 YAML 映射时,只是为了尝试一下,每当我尝试更新我的架构时都会收到此错误

[2015-12-07 13:20:33] 应用程序错误: Doctrine\Common\Persistence\Mapping\MappingException: 类 'Utils\DoctrineBundle\Entity\SluggableTrait' 不存在(未捕获 例外)在 /srv/myapp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php 运行控制台命令时的第 96 行 doctrine:schema:update {“异常”:“[对象] (学说\通用\持久性\映射\映射异常(代码:0): 类 'Utils\DoctrineBundle\Entity\SluggableTrait' 不存在 在 /srv/myapp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96)"}

                                                                               [Doctrine\Common\Persistence\Mapping\MappingException]                

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

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

我试图搜索 Trait YAML 示例但没有成功...所以我现在卡住了。

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

我创建了一个 PR 来遵循这个 https://github.com/doctrine/common/pull/701

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

【问题讨论】:

  • 你不应该是 ORM 用 @ORM/Entity()@ORM/Table(name="foo") 注释你的 Foo 类吗?
  • 不是必须的,Doctrine 会生成你的表名作为你的类名。但实际上在我的代码中我有 @ORM/Table(name="foo") 我只是没有在这里写。

标签: php symfony doctrine-orm


【解决方案1】:

简答:

您不能混合使用 YAML 和 Annotation 映射,并且 Traits 不适用于 YAML 映射。

【讨论】:

    猜你喜欢
    • 2012-02-11
    • 1970-01-01
    • 2013-08-21
    • 2017-06-06
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多