@MappedSuperclass vs普通抽象类 [英] @MappedSuperclass vs plain abstract class

查看:80
本文介绍了@MappedSuperclass vs普通抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为共享相同属性的几个实体创建了一个基类,我认为这是 @MappedSuperclass 的一个好用例:

I have created a base class for several entities that share the same properties, and I thought that it was a good use case for a @MappedSuperclass:

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\MappedSuperclass
 */
abstract class Invoiceable
{
    /**
     * @ORM\ManyToOne(targetEntity="Invoice")
     * @ORM\JoinColumn(name="invoiceId", referencedColumnName="id")
     *
     * @var Invoice|null
     */
    protected $invoice = null;

    /**
     * @ORM\ManyToOne(targetEntity="CreditNote")
     * @ORM\JoinColumn(name="creditNoteId", referencedColumnName="id")
     *
     * @var CreditNote|null
     */
    protected $creditNote = null;
}

但是,当我删除 @时,我感到很惊讶MappedSuperclass 注释,它仍然可以按预期工作。

However, I was surprised that when removing the @MappedSuperclass annotation, it still works as expected.

@MappedSuperclass 超类,那么,如果没有,它是否可以工作?

What is the purpose of @MappedSuperclass superclass then, if it works without?

推荐答案

礼貌教区用户邮件列表上的Marco Pivetta


这实际上只是基于注释驱动程序工作方式的幸运案例-同意,这很令人困惑。之所以起作用,是因为您的属性受到 的保护。

我建议您尝试对XML或YAML映射进行相同的操作-您将了解到崩溃严重。

I suggest you to try the same with XML or YAML mappings - you will see how it crashes badly.

您仍应将其定义为映射的超类。

这篇关于@MappedSuperclass vs普通抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆