/** * adds foreign key by relation instance * * @param \Dive\Relation\Relation $relation * @return Migration * @throws MigrationException */ public function addForeignKeyByRelation(Relation $relation) { if ($relation->getOwningTable() !== $this->tableName) { throw new MigrationException("\n Relation does not belong to table {$this->tableName}, it belongs to " . $relation->getOwningTable() . '!'); } $owningField = $relation->getOwningField(); $referencedTable = $relation->getReferencedTable(); $referencedField = $relation->getReferencedField(); $onDelete = $relation->getOnDelete(); $onUpdate = $relation->getOnUpdate(); return $this->addForeignKey($owningField, $referencedTable, $referencedField, $onDelete, $onUpdate); }
/** * @param Relation $owningRelation * @param Record $owningRecord * @param Record $referencedRecord * @throws UnitOfWorkException */ private function applyUpdateConstraint(Relation $owningRelation, Record $owningRecord, Record $referencedRecord) { $refFieldName = $owningRelation->getReferencedField(); if (!$referencedRecord->isFieldModified($refFieldName)) { return; } $owningFieldName = $owningRelation->getOwningField(); if ($owningRecord->isFieldModified($owningFieldName) && !$this->isRecordScheduledForCommit($owningRecord)) { $this->scheduleSave($owningRecord); return; } $constraintName = $owningRelation->getOnUpdate(); switch ($constraintName) { case PlatformInterface::CASCADE: $value = $referencedRecord->get($refFieldName); $owningRecord->set($owningFieldName, $value); if (!$this->isRecordScheduledForSave($owningRecord)) { $this->scheduleSave($owningRecord); } break; case PlatformInterface::SET_NULL: $owningRecord->set($owningFieldName, null); if (!$this->isRecordScheduledForSave($owningRecord)) { $this->scheduleSave($owningRecord); } break; case PlatformInterface::RESTRICT: case PlatformInterface::NO_ACTION: // TODO exception not specific enough? throw new UnitOfWorkException("Update record is restricted by onUpdate!"); } }