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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With