Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract Class for Doctrine entity

I am trying to make an abstract class for my entities involving general fields like created_at and updated_at values:

<?php
namespace AppBundle;

use Doctrine\ORM;

abstract class Model {

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

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

}

Then, I extend my class:

<?php
namespace AppBundle\Entity;

use AppBundle\Model;

/**
 * Entity
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="RB\ProductsBundle\Entity\ProductRepository")
 */
class Entity extends Model
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    private $name;
}

The problem is: When I make a php app/console doctrine:schema:update --dump-sql, it doesn't recognize the Model fields. How can I change that? Should I use a trait or something like that?

EDIT: I tried to add * @ORM\MappedSuperclass at the top of my abstract class with no luck

like image 385
Hammerbot Avatar asked Oct 05 '16 08:10

Hammerbot


1 Answers

The problem is likely to be that your properties in the abstract parent class are defined as private. Change them to protected and it should work.

As an aside, I use traits for this purpose, rather than inheritance, as your entity classes can use multiple traits, but they can only extend one abstract class.

like image 98
Will Avatar answered Oct 10 '22 14:10

Will