Note: this returns an UUID even if the object has not been persisted
in case of AOP-managed entities. Use isNewObject() if you need
to distinguish those cases.
public getIdentifierByObject ( object $object ) : string | ||
$object | object | |
return | string |
/** * Checks whether the given object is contained in the array. This checks * for object identity in terms of the persistence layer, i.e. the UUID, * when comparing entities. * * @param array $array * @param object $object * @param string $identifier * @return boolean */ protected function arrayContainsObject(array $array, $object, $identifier) { if (in_array($object, $array, true) === true) { return true; } foreach ($array as $value) { if ($value instanceof $object && $this->persistenceSession->getIdentifierByObject($value) === $identifier) { return true; } } return false; }
/** * Returns the (internal) identifier for the object, if it is known to the * backend. Otherwise NULL is returned. * * Note: this returns an identifier even if the object has not been * persisted in case of AOP-managed entities. Use isNewObject() if you need * to distinguish those cases. * * @param object $object * @return mixed The identifier for the object if it is known, or NULL * @api */ public function getIdentifierByObject($object) { return $this->persistenceSession->getIdentifierByObject($object); }
/** * @test */ public function getIdentifierByObjectReturnsValueOfPropertyTaggedWithId() { $object = $this->createMock(ProxyInterface::class); $object->Persistence_Object_Identifier = 'randomlyGeneratedUuid'; $object->customId = 'customId'; $mockReflectionService = $this->createMock(ReflectionService::class); $mockReflectionService->expects($this->any())->method('getPropertyNamesByTag')->will($this->returnValue(['customId'])); $session = new Persistence\Generic\Session(); $session->injectReflectionService($mockReflectionService); $this->assertEquals('customId', $session->getIdentifierByObject($object)); }