private function traverseToRelatedEntity(&$currentTable, &$currentTableAlias, Property $property) { if (!$property->hasRelationship()) { $entityClass = $this->mapper->getEntityClass($currentTable); throw new InvalidRelationshipException("Property '{$property->getName()}' in entity '{$entityClass}' doesn't have any relationship."); } $implicitFilters = array(); $propertyType = $property->getType(); if (is_subclass_of($propertyType, 'LeanMapper\\Entity')) { $caller = new Caller($this, $property); $implicitFilters = $this->mapper->getImplicitFilters($property->getType(), $caller); } $relationship = $property->getRelationship(); if ($relationship instanceof Relationship\HasOne) { $targetTable = $relationship->getTargetTable(); $targetTablePrimaryKey = $this->mapper->getPrimaryKey($targetTable); $referencingColumn = $relationship->getColumnReferencingTargetTable(); // Join table. $targetTableAlias = $this->joinRelatedTable($currentTableAlias, $referencingColumn, $targetTable, $targetTablePrimaryKey, $implicitFilters, FALSE); } elseif ($relationship instanceof Relationship\BelongsTo) { // BelongsToOne, BelongsToMany $targetTable = $relationship->getTargetTable(); $sourceTablePrimaryKey = $this->mapper->getPrimaryKey($currentTable); $referencingColumn = $relationship->getColumnReferencingSourceTable(); // Join table. $targetTableAlias = $this->joinRelatedTable($currentTableAlias, $sourceTablePrimaryKey, $targetTable, $referencingColumn, $implicitFilters); } elseif ($relationship instanceof Relationship\HasMany) { $sourceTablePrimaryKey = $this->mapper->getPrimaryKey($currentTable); $relationshipTable = $relationship->getRelationshipTable(); $sourceReferencingColumn = $relationship->getColumnReferencingSourceTable(); $targetReferencingColumn = $relationship->getColumnReferencingTargetTable(); $targetTable = $relationship->getTargetTable(); $targetTablePrimaryKey = $this->mapper->getPrimaryKey($targetTable); // Join tables. // Don't apply filters on relationship table. $relationshipTableAlias = $this->joinRelatedTable($currentTableAlias, $sourceTablePrimaryKey, $relationshipTable, $sourceReferencingColumn); $targetTableAlias = $this->joinRelatedTable($relationshipTableAlias, $targetReferencingColumn, $targetTable, $targetTablePrimaryKey, $implicitFilters, FALSE); } else { throw new InvalidRelationshipException('Unknown relationship type. ' . get_class($relationship) . ' given.'); } $currentTable = $targetTable; $currentTableAlias = $targetTableAlias; return $this->getPropertiesByTable($targetTable); }
/** * @param Property $property * @param string $mapperClass * @param Entity $entity * @throws InvalidValueException */ private function checkConsistency(Property $property, $mapperClass, Entity $entity) { $type = $property->getType(); if (!$entity instanceof $type) { throw new InvalidValueException("Inconsistency found: property '{$property->getName()}' in entity " . get_called_class() . " is supposed to contain an instance of '{$type}' (due to type hint), but mapper maps it to '{$mapperClass}'. Please fix getEntityClass method in mapper, property annotation or entities inheritance."); } }