为什么包继承在你的情况下没有用
如果您希望覆盖第三方包,并且主要是在您打算覆盖提供相同功能的所有文件时,包继承很有用。
例如,您可以覆盖第三方捆绑包FOSUserBundle,将其扩展为您的AcmeUserBundle,以自定义、改进、更改父捆绑包各个部分的逻辑。
但是同一个包必须提供相同的功能。所以“AcmeUserBundle 扩展了FOSUserBundle 因为它打算和FOSUserBundle 做同样的事情,例如add support for a database-backed user system in Symfony2”。
在您的情况下,BlogBundle 或 NewsBundle 与 PostBundle 没有任何共同之处,因此包继承不是这里的方法。
为您的实体使用简单的继承
对于您的特定情况,您只需要使用简单的继承,保留实体并不重要。 (事实上,您甚至可以have a single bundle,正如Elnur 所解释的那样。作为旁注,您还可以快速查看How do you organize your bundles in Symfony2?)。
Acme\PostBundle\Entity\Post
<?php
namespace Acme\PostBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Acme\PostBundle\Model\PostInterface;
/**
* @ORM\Entity
*/
class Post implements PostInterface
{
// ....
Acme\NewsBundle\Entity\NewsPost
<?php
namespace Acme\NewsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Acme\PostBundle\Entity\Post as BasePost;
/**
* @ORM\Entity
*/
class NewsPost extends BasePost
{
// ....
Acme\BlogBundle\Entity\BlogPost
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Acme\PostBundle\Entity\Post as BasePost;
/**
* @ORM\Entity
*/
class BlogPost extends BasePost
{
// ....