Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
VincentLanglet opened this issue Apr 6, 2021 · 5 comments

Comments

@VincentLanglet
Copy link
Contributor

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 ?

@beberlei
Copy link
Member

beberlei commented Apr 6, 2021

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

@VincentLanglet
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

It does work in a PreFlush event if I call

$args->getEntityManager()->getUnitOfWork()->computeChangeSet($args->getEntityManager()->getClassMetadata(Address::class), $leadAddress);

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Apr 9, 2021

I solved my issue, but I need computeChangeSet so I created #8600

thanks @beberlei

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants