Saving fresh added object results in infinite loop


#1

Hi,

after updating pimcore to version 5.3.1 I experience this weird behaviour:

I edited the PostAdd funcrtion in my TestListener. Now I add some values to different fields and when saving pimcore starts looping over and over.

Just for testing I removed all other code and just save the object (and write a Testmessage in a logfile).
The result is: Pimcore loads some time and after a few seconds I have a 100MB logfile and the error:
[17-Dec-2018 13:28:56 Europe/Berlin] PHP Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 262144 bytes) in /opt/ProDaS/src/AppBundle/EventListener/TestListener.php on line 35
[17-Dec-2018 13:28:56 Europe/Berlin] PHP Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0

This did not occur before upgrading.


#2

Hi, adding to this:
I indeed had some code that ran when Saving the same type of object. thought that the loop came because of this (Sounded weird to me, but hey, lets give it a try)
So I removed the code for Changes after Updating and it still occurs.

Just to make it clear, have the following code:

Blockquote
public function onObjectPostAdd(DataObjectEvent $event)
{
$object = $event->getObject();
if($object->gettype() != folder)
{
if($object->getclassname() == “Motiv”)
{
$object->save();
}
}
}

And BOOM the loop is here…how the f can this happen?


#3

Because you are calling object->save() from object->save()? OnObjectPostAdd is called in save function through the event handler.

Read the function definition of save and you’ll see why, or step through the function.


#4

But this behaviour is new, am I right?

I have another pimcore installation (version 5.1.1) and used the same code there. Everything is fine.

Is there a workaround for the newer version?

When I remove the $object->save just nothing happens, so PostAdd sounds rather useless.


#5

Hi,
if you want to change the very same object, I would use PreAdd/PreUpdate.
PostAdd can be used if some other entites/systems need to be notified when an object was added.

BR
Christian


#6

Hi,

so that means adding

  • { name: kernel.event_listener, event: pimcore.dataobject.preAdd, method: onObjectPreAdd }

to app/config/services.yml and changing:

public static function getSubscribedEvents()
{
return [
DataObjectEvents::PRE_UPDATE => ‘onObjectPreUpdate’,
DataObjectEvents::POST_ADD => ‘onObjectPostAdd’,
];
}

to

public static function getSubscribedEvents()
{
return [
DataObjectEvents::PRE_UPDATE => ‘onObjectPreUpdate’,
DataObjectEvents::PRE_ADD => ‘onObjectPreAdd’,
];
}

?

I tried this steps and now NOTHING happens.


Error with gallery configuration
#7

Was this the correct way of implementing it? Or did I miss something?


#8

Still the same problem


#9

Can you share your services.yml? Where do you have you listener placed? And why do you use getSubscribedEvents?