Doctrine lifecycle callbacks don’t fire
I want to create an identifier for my invoices, after I persist them. I’m using an auto generated ID in my identifier, so I have to use some postPersist callback to achieve this. However, my postPersist callback doesn’t get fired.
Here is my AbstractInvoice entity:
<?php
namespace AppInvoice;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMHasLifecycleCallbacks
* @ORMTable("invoices")
* @ORMInheritanceType("SINGLE_TABLE")
* @ORMDiscriminatorColumn(name="type", type="integer")
* @ORMDiscriminatorMap({
* "0"="Regular",
* "1"="Credit"
* })
*/
abstract class AbstractInvoice
{
/**
* @ORMId()
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
* @var int
*/
protected $id;
/**
* @ORMColumn(type="string")
* @var string
*/
protected $identifier;
/**
* @ORMPostPersist
*/
protected function onPersist(): void
{
$this->identifier = '1' . str_pad($this->id, 5, '0', STR_PAD_LEFT);
}
}
And here is my Regular invoice entity:
<?php
namespace AppInvoice;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
*/
class Regular extends AbstractInvoice
{
}
I create an instance of the regular invoice entity in my controller:
<?php
namespace App;
use AppInvoiceRegular;
class Controller
{
public function handle(InvoiceRepository $invoices)
{
$invoice = new Regular();
$invoices->add($invoice);
}
}
Which uses this repository:
<?php
namespace App;
use AppInvoiceAbstractInvoice;
use DoctrineORMEntityRepository;
class InvoiceRepository extends EntityRepository
{
public function add(AbstractInvoice $invoice): void
{
$this->_em->persist($invoice);
$this->flush();
}
}
The invoice gets stored fine, only the onPersist method doesn’t get fired. I already tried implementing the callback in the sub class (Regular), switching to a public method. No success there. Am I missing something?
Thank you!
from Laravel Questions and Answers https://laravelquestions.com/php/doctrine-lifecycle-callbacks-dont-fire/
via Lzo Media
No comments:
Post a Comment