Reputation: 5095
I'm new to Symfony (I'm used to ZF1 and ZF2), and just got started looking into it for a new project. I'm at the stage of dividing the application into bundles and while doing this I'm seeing a first hurdle to take. The application consists of several bundles that will be using posts in one way or another, but each type is a little different. For example I would have a news bundle with a NewsPost entity and another bundle blog with an entity blogpost. Of course I could use separate entity classes without using a superclass, but it seems neater to have a superclass Post and just create subclasses inside of each bundle. Especially because every post class will have the same relationships with other entity classes (think Tag for example).
I've read about bundle inheritance in Symfony, but it doesn't seem to be the way to go here, since I only want to subclass the entity class and also it looks like it's only possible to have one sub-bundle per parent bundle. I prefer keeping the bundles separate, because they will contain features that are unrelated to eachother. What would be a good way to handle a scenario like this?
Upvotes: 2
Views: 2877
Reputation: 31959
Bundle inheritance is useful if you wish to override a third party bundle, and mainly if what you intend to do is overriding a all bunch of files that serve the same functionality.
For example, you would override the third party bundle FOSUserBundle
by extending it into your AcmeUserBundle
to customise, improve, change the logic of various parts of the parent bundle.
But that same bundle has to serve the same functionality. So "AcmeUserBundle
extends FOSUserBundle
becuase it intends to do the same as FOSUserBundle
, e.g add support for a database-backed user system in Symfony2".
In your case BlogBundle
or NewsBundle
have nothing in common with PostBundle
, so bundle inheritance is not the way to go here.
For your particular case, you just have to use simple inheritance, where you keep your entities doesn't really matter. (As a matter of fact you could even have a single bundle, as beautifully explained by Elnur. As a side note, you could also have a quick look at 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
{
// ....
Upvotes: 3