I have model that allows only to read property CreatedAt
. This field is generated by SQL.
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue
*/
private $Id;
...
/**
* @ORMColumn(type="datetime", insertable=false, updatable=false)
*/
private $CreatedAt;
It works fine exept 1 case: I can’t get that field upon creation:
$newObject = NewClassName();
$this->entityManager->persist($newObject);
$this->entityManager->flush();
$newObject->getId(); \ = 1234 (some generated integer)
$newObject->getCreatedAt(); \ = null
So question is: how to meke doctrine to autofill this field upon creation like it happens to $Id
field
I’ve tried adding @GeneratedValue
– does not work;
For now I use detach
and getting it from DB after creation:
$newObject = NewClassName();
$this->entityManager->persist($newObject);
$this->entityManager->flush();
if ($newObject->getId()) {
$this->entityManager->detach($newObject);
$newObject = $this->entityManager->getRepository(NewClassName::class)->find($newObject->getId());
$newObject->getCreated(); \ = DateTime object
But I guess there is a better way. Thanks forahead!