@MappedSuperclass vs plain abstract class

て烟熏妆下的殇ゞ 提交于 2020-01-25 11:56:06

问题


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

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

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


回答1:


Courtesy Marco Pivetta on the doctrine-user mailing list:

This is really just a lucky case based on how the annotation driver works - agreed, it is confusing. It works because your properties are protected.

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

You should still define that as a mapped superclass.



来源:https://stackoverflow.com/questions/21919954/mappedsuperclass-vs-plain-abstract-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!