/** * 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 * @throws UnitOfWorkException */ private function applyDeleteConstraint(Relation $owningRelation, Record $owningRecord) { $owningFieldName = $owningRelation->getOwningField(); if ($owningRecord->isFieldModified($owningFieldName)) { return; } $constraintName = $owningRelation->getOnDelete(); switch ($constraintName) { case PlatformInterface::CASCADE: $this->scheduleDelete($owningRecord); break; case PlatformInterface::SET_NULL: if (!$this->isRecordScheduledForDelete($owningRecord)) { $owningRecord->set($owningFieldName, null); $this->scheduleSave($owningRecord); } break; case PlatformInterface::RESTRICT: case PlatformInterface::NO_ACTION: // if not deleted yet, it has to be deleted before commit for comply with the constraint if (!$this->isRecordScheduledForDelete($owningRecord)) { $this->markRecordForRestrictOnCommitWhenNotDeleted($owningRecord); } break; } }