How to get the internal persistence identifier of an object?


(Christian Müller) #1

Flow doesn’t expose the technical identifier of entities by default as it is generally only for internal use. There are still times you want to get hold of it and there are several ways to do that:

Inject yourself the PersistenceManager \TYPO3\Flow\Persistence\PersistenceManagerInterface which then can give you the identifier:

$identifier = $this->persistenceManager->getIdentifierByObject($object);

in Fluid templates the f:format.identifier viewhelper exists which can be used like this for example:

{object -> f:format.identifier()}

(Alexander Berl) #2

Or, if the identifier of your objects happens to be an important part of your domain logic, you should make it explicit in your models by creating the specific @ORM\Id properties and creating a proper getter for it.


(Lorenz Ulrich) #3

For getting the identifier in Fusion/TypoScript, I wrote a little Eel Helper (working in Neos 2.3 LTS, but can be easily adapted):

<?php
namespace My\Site\Eel\Helper;

use TYPO3\Eel\ProtectedContextAwareInterface;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Persistence\PersistenceManagerInterface;

class ObjectHelper implements ProtectedContextAwareInterface
{

    /**
     * @var PersistenceManagerInterface
     * @Flow\Inject
     */
    protected $persistenceManager;

    /**
     * @param $object Object
     * @return string
     */
    public function identifier($object) {
        return $this->persistenceManager->getIdentifierByObject($object);
    }

    /**
     * All methods are considered safe
     *
     * @param string $methodName
     * @return boolean
     */
    public function allowsCallOfMethod($methodName)
    {
        return true;
    }

}

Now I register the helper in the settings of my package:

  TypoScript:
    defaultContext:
      'My.Object': 'My\Site\Eel\Helper\ObjectHelper'

And use it in Fusion/TypoScript:


prototype(My.Site:FileArchive) {
	@context.assetCollection = ${q(node).property('assetCollection')}

	@cache {
		mode = 'cached'
		maximumLifetime = '86400'
		entryTags {
			1 = ${'Node_' + node.identifier}
			2 = ${'Neos.Media.AssetCollection_' + My.Object.identifier(assetCollection)}
		}
	}
}