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); } }); } }
public function ForeignKey(QueryBuilder $QueryBuilder, Table $Table, Traits\ForeignKey $Trait) { $ColumnNameMap = $Trait->GetParentReferencedColumnNameMap(); $PrimaryColumns = array_keys($ColumnNameMap); $ForeignColumns = array_values($ColumnNameMap); $QueryBuilder->AppendIdentifier('CONSTRAINT # ', [$Trait->GetName()]); $QueryBuilder->AppendIdentifiers('FOREIGN KEY (#) ', $PrimaryColumns, ','); $QueryBuilder->AppendIdentifier('REFERENCES # ', [$Trait->GetReferencedTable()->GetName()]); $QueryBuilder->AppendIdentifiers('(#) ', $ForeignColumns, ','); $QueryBuilder->Append('ON UPDATE ' . $this->MapForeignKeyMode($Trait->GetUpdateMode()) . ' '); $QueryBuilder->Append('ON DELETE ' . $this->MapForeignKeyMode($Trait->GetDeleteMode()) . ' '); }