Set persistence_object_identifier manually

Hey guys,

is there a way to set the persistence_object_identifier manually?
The thing is that I import a CSV, in which the identifier are already inside.

...
$data = new \My\App\Domain\Model\Myitem();
$data->setIdentifier($identifier);
...

Thx,
Pat

Hi,

I don’t really know, but …

  • Libraries: utility-objecthandling: ObjectAccess ??

public static function setProperty(&$subject, $propertyName, $propertyValue, bool $forceDirectAccess = false) …


use Neos\Utility\ObjectAccess;

ObjectAccess::setProperty($yourObject, ‘persistence_object_identifier’, $yourIdentifier, true);


Maybe someone who really knows could confirm if this would be the way to go - or not … ?

Christoph

Yes, you can set it via ObjectAccess hacks as above. It will however blow up if Flow ever decides to change the magic persistence identifier, since you are coupling to an implementation detail of the Framework.

If this CSV import is a normal business action and not just a onetime solution the better way would be, to make it explicit! Add an own identifier property to your model (@ORM\Id) and provide a constructor (!identifier should not be changeable after construction) that allows to set the identifier. If you are bound to the current table schema, you could still map your own property to the persistence_object_identifier column via Doctrine annotation.

Hey Alexander and Christoph!

Thank you for your answers. I have tried it now with ObjectAccess. The data is imported correctly but the identifier is omitted.

I try to import a csv in which several records are included that already have an identifier. Btw. This is a unique job.

foreach ($animallist as $animal) {
      list($identifier,$name,$lab) = explode(';', $animal);

      $animaldata = new \AlpacaAustria\AlpacaConnect\Domain\Model\Animal();
      $animaldata->setName($name);
      $animaldata->setLab($lab);

      ObjectAccess::setProperty($animaldata, 'persistence_object_identifier', $identifier, true);
      
      $this->animalRepository->add($animaldata);

}

As a alternative, you can mark a property with @ORM\Id and have a getter/setter for that property, and then voila - no hacky hacky :slight_smile:

ObjectAccess::setProperty($animaldata, ‘persistence_object_identifier’, $identifier, true);

The property in PHP is actually written in uppercase Persistence_Object_Identifier, only the table column is then converted to lowercase. Another reason this isn’t a good idea to do generally, but if it’s really just for a one-time job, go ahead :slight_smile: