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.
public getIdentifierByObject ( object $object ) : mixed | ||
$object | object | |
return | mixed | The identifier for the object if it is known, or NULL |
/** * @test */ public function getIdentifierByObjectUsesUnitOfWorkIdentityWithEmptyFlowPersistenceIdentifier() { $entity = (object) ['Persistence_Object_Identifier' => null]; $this->mockEntityManager->expects($this->any())->method('contains')->with($entity)->will($this->returnValue(true)); $this->mockUnitOfWork->expects($this->any())->method('getEntityIdentifier')->with($entity)->will($this->returnValue(['SomeIdentifier'])); $this->assertEquals('SomeIdentifier', $this->persistenceManager->getIdentifierByObject($entity)); }
/** * @param DoctrineSqlFilter $sqlFilter * @param QuoteStrategy $quoteStrategy * @param ClassMetadata $targetEntity * @param string $targetTableAlias * @param string $targetEntityPropertyName * @return string * @throws InvalidQueryRewritingConstraintException * @throws \Exception */ protected function getSqlForManyToOneAndOneToOneRelationsWithPropertyPath(DoctrineSqlFilter $sqlFilter, QuoteStrategy $quoteStrategy, ClassMetadata $targetEntity, $targetTableAlias, $targetEntityPropertyName) { $subselectQuery = $this->getSubselectQuery($targetEntity, $targetEntityPropertyName); $associationMapping = $targetEntity->getAssociationMapping($targetEntityPropertyName); $subselectConstraintQueries = []; foreach ($associationMapping['joinColumns'] as $joinColumn) { $rootAliases = $subselectQuery->getQueryBuilder()->getRootAliases(); $subselectQuery->getQueryBuilder()->select($rootAliases[0] . '.' . $targetEntity->getFieldForColumn($joinColumn['referencedColumnName'])); $subselectSql = $subselectQuery->getSql(); foreach ($subselectQuery->getParameters() as $parameter) { $parameterValue = $parameter->getValue(); if (is_object($parameterValue)) { $parameterValue = $this->persistenceManager->getIdentifierByObject($parameter->getValue()); } $subselectSql = preg_replace('/\\?/', $this->entityManager->getConnection()->quote($parameterValue, $parameter->getType()), $subselectSql, 1); } $quotedColumnName = $quoteStrategy->getJoinColumnName($joinColumn, $targetEntity, $this->entityManager->getConnection()->getDatabasePlatform()); $subselectIdentifier = 'subselect' . md5($subselectSql); $subselectConstraintQueries[] = $targetTableAlias . '.' . $quotedColumnName . ' IN (SELECT ' . $subselectIdentifier . '.' . $joinColumn['referencedColumnName'] . '_0 FROM (' . $subselectSql . ') AS ' . $subselectIdentifier . ' ) '; } return ' (' . implode(' ) AND ( ', $subselectConstraintQueries) . ') '; }