protected function PersistRelationshipChanges(Object\Domain $Domain, Object\UnitOfWork $UnitOfWork, $ParentEntity, $CurrentValue, $HasOriginalValue, $OriginalValue)
{
$RelationshipChanges = [];
$OriginalEntities = [];
$CurrentEntities = [];
if (!$CurrentValue instanceof Collections\ICollection) {
if (!$CurrentValue instanceof \Traversable) {
throw new Object\ObjectException('Invalid value for collection property on entity %s, Traversable expected, %s given', $this->GetEntityMap()->GetEntityType(), \Storm\Core\Utilities::GetTypeOrClass($CurrentValue));
}
foreach ($CurrentValue as $Entity) {
if ($this->IsValidEntity($Entity)) {
$CurrentEntities[] = $Entity;
}
}
} else {
if ($CurrentValue instanceof Collections\LazyCollection && !$CurrentValue->__IsLoaded()) {
return [];
} else {
if (!$CurrentValue->__IsAltered()) {
foreach ($CurrentValue->ToArray() as $Entity) {
if ($Entity instanceof Proxies\IProxy && !$Entity->__IsAltered()) {
continue;
}
$UnitOfWork->PersistRelationships($Entity);
}
return [];
} else {
$CurrentEntities = $CurrentValue->ToArray();
}
}
}
if ($HasOriginalValue) {
$OriginalEntities = $OriginalValue->ToArray();
}
$NewOrAlteredEntities = $this->ComputeDifference($CurrentEntities, $OriginalEntities);
$RemovedEntities = $this->ComputeIdentityDifference($Domain, $OriginalEntities, $CurrentEntities);
foreach ($NewOrAlteredEntities as $NewEntity) {
$RelationshipChanges[] = new Object\RelationshipChange($this->RelationshipType->GetPersistedRelationship($Domain, $UnitOfWork, $ParentEntity, $NewEntity), null);
}
foreach ($RemovedEntities as $RemovedEntity) {
$RelationshipChanges[] = new Object\RelationshipChange(null, $this->RelationshipType->GetDiscardedRelationship($Domain, $UnitOfWork, $ParentEntity, $RemovedEntity));
}
return $RelationshipChanges;
}