Skip to content

Instantly share code, notes, and snippets.

@meshenka
Created July 19, 2019 09:33
Show Gist options
  • Save meshenka/990991bcde274f60a3f522f4ef85f360 to your computer and use it in GitHub Desktop.
Save meshenka/990991bcde274f60a3f522f4ef85f360 to your computer and use it in GitHub Desktop.
onFlush Doctrine event
An exception occurred while executing 'INSERT INTO member_product (member_id, label, date, blocked, desactivate_2fa_until, member_id_fk) VALUES (?, ?, ?, ?, ?, ?)' with params [null, "app-p2p", "2011-0
9-11 12:39:11", 0, "-0001-11-30 00:00:00", "3"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'member_id' cannot be null
<?php
declare(strict_types=1);
namespace App\Membership\Entity;
use App\Application\Annotation\Legacy;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(
* name="member_product",
* uniqueConstraints={
* @ORM\UniqueConstraint(name="idx_fk", columns={"member_id_fk", "label"}),
* @ORM\UniqueConstraint(name="idx", columns={"member_id", "label"})
* },
* indexes={
* @ORM\Index(name="label", columns={"label"})
* })
* @ORM\Entity(repositoryClass="App\Membership\Repository\MemberProductRepository")
*/
class MemberProduct
{
//...
/**
* @Legacy
*/
public function replicate(): void
{
if ($member = $this->getMember()) {
$this->memberId = $member->getId();
}
}
}
<?php
declare(strict_types=1);
namespace App\Membership\Legacy;
use App\Application\Annotation\Legacy;
use App\Membership\Entity\MemberProduct;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
/**
* @Legacy
*/
class MemberProductFkListener
{
public function onFlush(MemberProduct $product, OnFlushEventArgs $event)
{
dump('here'); // does not goes here
$product->replicate();
$entityManager = $event->getEntityManager();
$uow = $entityManager->getUnitOfWork();
$classMetadata = $entityManager->getClassMetadata(MemberProduct::class);
$uow->recomputeSingleEntityChangeSet($classMetadata, $product);
}
}
App\Membership\Legacy\MemberProductFkListener:
autowire: true
tags:
- { name: doctrine.orm.entity_listener, event: onFlush, entity: App\Membership\Entity\MemberProduct, lazy: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment