Creating an entity inside a PreFlush is throwing an error INSERT VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) #6682

Closed
opened 2026-01-22 15:36:58 +01:00 by admin · 5 comments
Owner

Originally created by @VincentLanglet on GitHub (Apr 6, 2021).

I have an entity Subscription in OtO with Address and Lead which is also in OtO with Address.

In the Subscription, I have the following preFlush

    /**
     * @param PreFlushEventArgs $args
     *
     * @return void
     *
     * @ORM\PreFlush()
     */
    public function updateLeadValue(PreFlushEventArgs $args)
    {
        $lead = $this->getLead();
        if (null === $lead) {
            return;
        }

        $accessor = new PropertyAccessor();
        $address = $this->getAddress();
        if (null === $address) {
            return;
        }

        $addressHasChanged = false;
        $originalData = $args->getEntityManager()->getUnitOfWork()->getOriginalEntityData($address);
        unset($originalData['id']);

        foreach ($originalData as $fieldName => $value) {
            if ($value !== $accessor->getValue($address, $fieldName)) {
                $addressHasChanged = true;
                break;
            }
        }

        if (!$addressHasChanged) {
            return;
        }

        $leadAddress = $lead->getAddress() ?? new Address();
        foreach ($originalData as $fieldName => $value) {
            $accessor->setValue($leadAddress, $fieldName, $accessor->getValue($address, $fieldName));
        }
        $lead->setAddress($leadAddress);

        $args->getEntityManager()->persist($leadAddress);
        $args->getEntityManager()->persist($lead);
    }

Everything works fine when $lead->getAddres() is not null ; the address is updated.
But when it's null (ie the code new Address(); is executed), I was expecting that a new address could be created and set.
Instead, I'm getting the following error:

An exception occurred while executing 'INSERT INTO asv_address (firstname, usagename, gender, address_first_line, address_second_line, address_third_line, address_fourth_line, zipcode, city, country_iso_code, insee_code, certified_by_la_poste) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)':

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' at line 1

Is this a bug ?

Originally created by @VincentLanglet on GitHub (Apr 6, 2021). I have an entity `Subscription` in OtO with `Address` and `Lead` which is also in OtO with `Address`. In the Subscription, I have the following preFlush ``` /** * @param PreFlushEventArgs $args * * @return void * * @ORM\PreFlush() */ public function updateLeadValue(PreFlushEventArgs $args) { $lead = $this->getLead(); if (null === $lead) { return; } $accessor = new PropertyAccessor(); $address = $this->getAddress(); if (null === $address) { return; } $addressHasChanged = false; $originalData = $args->getEntityManager()->getUnitOfWork()->getOriginalEntityData($address); unset($originalData['id']); foreach ($originalData as $fieldName => $value) { if ($value !== $accessor->getValue($address, $fieldName)) { $addressHasChanged = true; break; } } if (!$addressHasChanged) { return; } $leadAddress = $lead->getAddress() ?? new Address(); foreach ($originalData as $fieldName => $value) { $accessor->setValue($leadAddress, $fieldName, $accessor->getValue($address, $fieldName)); } $lead->setAddress($leadAddress); $args->getEntityManager()->persist($leadAddress); $args->getEntityManager()->persist($lead); } ``` Everything works fine when `$lead->getAddres()` is not null ; the address is updated. But when it's null (ie the code `new Address();` is executed), I was expecting that a new address could be created and set. Instead, I'm getting the following error: ``` An exception occurred while executing 'INSERT INTO asv_address (firstname, usagename, gender, address_first_line, address_second_line, address_third_line, address_fourth_line, zipcode, city, country_iso_code, insee_code, certified_by_la_poste) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)': SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' at line 1 ``` Is this a bug ?
admin closed this issue 2026-01-22 15:36:58 +01:00
Author
Owner

@beberlei commented on GitHub (Apr 6, 2021):

No, not every event can do everything, you probably need to do this in the general onFlush event

@beberlei commented on GitHub (Apr 6, 2021): No, not every event can do everything, you probably need to do this in the general `onFlush` event
Author
Owner

@VincentLanglet commented on GitHub (Apr 7, 2021):

I just discovered that if the address of the subscription is new, it always works.
But if the address already exist for the subscription and is updated, it doesnt work if I have to create a new address.

I would have prefer to avoid creating a Listener and playing more with the UnitOfWork API. According to the doc, I'll need computeChangeSet and recomputeSingleEntityChangeSet, but these methods are marked as @internal.

@VincentLanglet commented on GitHub (Apr 7, 2021): I just discovered that if the address of the subscription is new, it always works. But if the address already exist for the subscription and is updated, it doesnt work if I have to create a new address. I would have prefer to avoid creating a Listener and playing more with the `UnitOfWork` API. According to the doc, I'll need `computeChangeSet` and `recomputeSingleEntityChangeSet`, but these methods are marked as `@internal`.
Author
Owner

@VincentLanglet commented on GitHub (Apr 7, 2021):

No, not every event can do everything, you probably need to do this in the general onFlush event

I just tried an onFlush event.
The issue I get is that $unitOfWork->getOriginalEntityData() is not returning the old value anymore...

@VincentLanglet commented on GitHub (Apr 7, 2021): > No, not every event can do everything, you probably need to do this in the general `onFlush` event I just tried an `onFlush` event. The issue I get is that `$unitOfWork->getOriginalEntityData()` is not returning the old value anymore...
Author
Owner

@VincentLanglet commented on GitHub (Apr 7, 2021):

It does work in a PreFlush event if I call

$args->getEntityManager()->getUnitOfWork()->computeChangeSet($args->getEntityManager()->getClassMetadata(Address::class), $leadAddress);
@VincentLanglet commented on GitHub (Apr 7, 2021): It does work in a PreFlush event if I call ``` $args->getEntityManager()->getUnitOfWork()->computeChangeSet($args->getEntityManager()->getClassMetadata(Address::class), $leadAddress); ```
Author
Owner

@VincentLanglet commented on GitHub (Apr 9, 2021):

I solved my issue, but I need computeChangeSet so I created https://github.com/doctrine/orm/pull/8600

thanks @beberlei

@VincentLanglet commented on GitHub (Apr 9, 2021): I solved my issue, but I need `computeChangeSet` so I created https://github.com/doctrine/orm/pull/8600 thanks @beberlei
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6682