【问题标题】:Doctrine Embeddables that share a common interface共享通用接口的 Doctrine Embeddables
【发布时间】:2015-02-22 00:34:55
【问题描述】:

使用 Doctrine Embeddables 时,您必须像这样在属性上声明一个类...

/** @Entity */
class User
{
    /** @Embedded(class = "Address") */
    private $address;
}

/** @Embeddable */
class Address
{
}

如果您的属性包含一个实现接口的类,会发生什么情况。例如......我有一个名为 Monthly 或 Weekly 的类,它实现了一个名为 RecurrenceInterface 的接口,我的实体看起来像这样......

/** @Entity */
class Subscription
{
    /** @var RecurrenceInterface */
    private $recurrence;
}

/** @Embeddable */
class Monthly implements RecurrenceInterface
{
}

如何将实现接口的东西声明为 Embeddable?

谢谢!

【问题讨论】:

    标签: php doctrine-orm doctrine


    【解决方案1】:

    Doctrines 教程说“可嵌入对象是本身不是实体的类”。课程,仅此而已..

    作为一种解决方案,您可以像这样将序列化对象存储在您的实体中

     /**
     * @var Settings\SettingsInterface
     * @ORM\Column(name="settings", type="object")
     */
    private $settings;
    

    在这种情况下,原则t cares what you store there, and you dont 不关心存储对象的序列化/反序列化。

    此外,您的类中的 setter、getter、on 构造等仍有类型提示。

     public function __construct(Settings\SettingsInterface $formSettings)
    

    这种方案有很多明显的缺点,比如在改变可序列化对象时

    【讨论】:

      猜你喜欢
      • 2015-11-23
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 2012-07-12
      • 2018-12-20
      相关资源
      最近更新 更多