gpt4 book ai didi

annotations - Symfony/Doctrine : Traits and Annotations

转载 作者:行者123 更新时间:2023-12-02 01:27:29 28 4
gpt4 key购买 nike

我有一个实体Test,它将从特征中获取它的属性(和基本方法):

class Test {
use Trait_title;
}

trait Trait_title{
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $title;
}

它工作正常。但是,当我尝试将注释放在 use 语句前面的 Test Class 中时,部分或完整的注释在我尝试更新架构时会被 symfony 忽略:

class Test {
/**
* @ORM\Column(type="string", length=255, nullable=false) //will be ignored...
*/
use Trait_title;
}

trait Trait_title {
private $title;
}

这样做的目的是将 Doctrine 注释的默认值移到特征中,但也允许为每个实体设置一些自定义注释,如nullable

最佳答案

您正在寻找的是映射覆盖。

你应该看看 Doctrine 官方文档来实现这个: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/override-field-association-mappings-in-subclasses.html

还有一个完全适合您的用例的示例,覆盖特征映射信息。

Sometimes also the mapping to override comes from entities using traits where the traits have mapping metadata. This tutorial explains how to override mapping metadata, i.e. attributes and associations metadata in particular.

第二种方法是通过重新定义映射信息来覆盖特征属性。有关此解决方案的更多详细信息,请参阅此答案: https://stackoverflow.com/a/11939306/4829152

关于annotations - Symfony/Doctrine : Traits and Annotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36224000/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com