public function Persist(Relational\Transaction $Transaction, Relational\ResultRow $ParentData, array $RelationshipChanges) { /* @var $RelationshipChanges Relational\RelationshipChange[] */ $JoinRowsWithoutPrimaryKeys = []; foreach ($RelationshipChanges as $RelationshipChange) { if ($RelationshipChange->HasPersistedRelationship()) { $JoinRow = $this->MapPersistedRelationshipToJoinRow($RelationshipChange->GetPersistedRelationship()); $Transaction->Persist($JoinRow); if (!$JoinRow->HasPrimaryKey()) { $JoinRowsWithoutPrimaryKeys[] = $JoinRow; } } if ($RelationshipChange->HasDiscardedRelationship()) { $JoinRow = $this->MapDiscardedRelationshipToJoinRow($RelationshipChange->GetDiscardedRelationship()); $Transaction->Discard($JoinRow->GetPrimaryKey()); } } if (count($JoinRowsWithoutPrimaryKeys) > 0) { $ParentRow = $ParentData->GetRow($this->ParentForeignKey->GetReferencedTable()); $Transaction->SubscribeToPrePersistEvent($this->JoinTable, function () use(&$ParentRow, &$JoinRowsWithoutPrimaryKeys) { foreach ($JoinRowsWithoutPrimaryKeys as $JoinRowWithoutPrimaryKey) { $this->ParentForeignKey->MapReferencedToParentKey($ParentRow, $JoinRowWithoutPrimaryKey); } }); } }
/** * @return Relational\ResultRow */ protected function MapParentRowToRelatedKey(ForeignKey $ForeignKey, Relational\ResultRow $ParentRow) { if ($this->IsInversed) { $ParentKey = $ForeignKey->ParentKey(); $ForeignKey->MapReferencedToParentKey($ParentRow, $ParentKey); return $ParentKey; } else { $ReferencedKey = $ForeignKey->ReferencedKey(); $ForeignKey->MapParentToReferencedKey($ParentRow, $ReferencedKey); return $ReferencedKey; } }