Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 - extend a base entity class or keep entities unrelated?

Tags:

symfony

I have a symfony2 project which includes half a dozen major entity types. I'm using Doctrine.

These entities have a number of identical fields, such as creatorId, Created, Updated, Status, and Title. At the moment, each entity is separately defined - so of course there's much duplication of basic code (and db fields as well).

I am learning OOP as I learn symfony2, so I'm groping here, but my question is this:

Is it considered best practice to keep each entity as a self-contained class? Or would it be preferable to create a new base class for the shared properties, and then have the other entities extend the base entity?

bonus question:

And if it is in fact better to build entities that inherit from a parent class, I can vaguely visualize two options:

1) the parent class is a fully mapped Doctrine entity, with its own table in the database, which we will call, oh let's call it "Nodes". So calling a child entity will always involve an additional Join between the Nodes table and the Child content type entity table.

2) the parent class is an - um - abstract class (?), defining the shared properties of the other entities, but not having an actual database presence. Each child entity implements the shared properties separately, so that the DB structure remains identical to my current setup, but there is (presumably) less duplication of code when defining entities.

I'm mostly looking for advice on the overall question - child entities extending a base entity, or just separate entities. Not expecting anyone to explain the best implementation though hints are welcome.

like image 476
ex future drupaller Avatar asked Jan 16 '14 07:01

ex future drupaller


1 Answers

I would create an abstract base entity class and let your other entities extend it. For example:

abstract class AbstractEntity
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     */
    protected $id;

    /**
     * @var \datetime $created
     *
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @var \datetime $updated
     *
     * @ORM\Column(type="datetime")
     */
    private $updated;

...

}

Then each of your entities can extend this:

class SomeEntity extends AbstractEntity
{

    /**
     * @var string
     *
     * @ORM\Column(name="some_name", type="string", length=255)
     */
    protected $something
}

Option 2 is the correct one - there would not be any concrete table for the abstract class in the database.

You could still have other abstract classes which extend the base class if it was required. For example, an AbstractVehicle entity would extend the base entity. If, for example, you wanted all sub-entities of AbstractVehicle (e.g. Car, Van, etc.) in the same table (e.g. 'vehicle'), you could use something like a Discriminator Map. This article on Inheritence Mapping might be useful reading.

like image 73
redjam13 Avatar answered Sep 28 '22 02:09

redjam13