BlackbitDigitalCommerce / pimcore-data-director

Import Bundle for Pimcore
16 stars 3 forks source link

DataObjectEvents::PRE_ADD event #87

Closed ascheider closed 1 year ago

ascheider commented 1 year ago

Pimcore has many standard data object events, where things can be manipulated. We have event for the customer, where we use pre add event, to generate uuid. This is done one time on creation. If we import objects with data director, the uuid is missing, because the event is not triggered from data director. Unfortunately i can not use callback function to generate uuid's like Uid::v4()->__toString(), because the will be overwritten if i execute import more than once. is it possible to add/fire pre add event, if object is fresh created via data director? Thanks!

BlackbitDevs commented 1 year ago

Which Pimcore event exactly do you mean? Currently the preAdd, preUpdate and postUpdate events for the corresponding target element type do get triggered, so in your case https://github.com/pimcore/pimcore/blob/d4d577dec5487130cb2c5bd62993080df7438267/lib/Event/DataObjectEvents.php#L25, right?

ascheider commented 1 year ago

@BlackbitDevs i debuged the issue. You are right, the event is called. The problem is, after uuid generation i set the uuid with setter, but do not save object, because it is saved by core afterwards:

$uuId = \str_replace('-', '', Uid::v4()->__toString()); $obj->setInternalid($uuId);

So if i add the object via admin, the uuid is set and saved to the object. But if i import the object and set uuid, data director does not persists this data afterwards and the uuid field is empty in the object.

BlackbitDevs commented 1 year ago

Correct, the reason is the optimized saving - only mapped import fields get saved while Pimcore always saves all fields (which makes it slow). Now there are 2 options for you to get the UUID saved:

  1. Map it in the dataport. You can enable the checkbox "Do not overwrite if already filled" to only generate it at the beginning - or you check for $params['currentObjectData']['id']: if it is empty, a new object got created instead of updating an existing one.
  2. Enable "Compatibility mode" in the advanced dataport settings. With this the default saving mechanism of Pimcore will get used - resulting in less performance but fully compliant processing.
ascheider commented 1 year ago

@BlackbitDevs Nice. Thanks. I will try it 👍