Anonymous Anonymous - 6 months ago
425 0

When your Symfony2 entity already extends a class and you need one more you can use a trait class, because php doesn't allow multiple inheritance from abstract classes.

You can use it like:

namespace My\Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use My\Bundle\Entity\TraitEntity;

/**
* Entity
*
* @ORM\Table(name="table")
*/
class Entity extends AbstractEntity
{

use TraitEntity;

.......

PHP

Symfony2 entity extend abstract class multiple inheritance with Doctrine

<?php

namespace My\Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\MappedSuperclass;

/**
 * trait class for entities that need more functionality and already extend an other class.
 *
 * @MappedSuperclass
 */
trait TraitEntity
{

    /**
     * @var integer
     *
     * @ORM\Column(name="column_x", type="integer", nullable=true)
     * @Assert\Type(type="integer")
     */
    protected $columnX = 0;

    /**
     * @return int
     */
    public function getColumnX()
    {
        return $this->columnX;
    }

    /**
     * @param int $columnX
     */
    public function setColumnX($columnX)
    {
        $this->columnX = $columnX;
    }

}