示例#1
0
 /**
  * Updates references between owner and referenced record collection
  *
  * @param RecordCollection|Record[] $ownerCollection
  * @param RecordCollection|Record[] $referencedCollection
  */
 public function updateOwnerCollectionWithReferencedCollection(RecordCollection $ownerCollection, RecordCollection $referencedCollection)
 {
     $owningField = $this->relation->getOwningField();
     $isOneToMany = $this->relation->isOneToMany();
     foreach ($ownerCollection as $refRecord) {
         $refId = $refRecord->get($owningField);
         $owningId = $refRecord->getInternalId();
         $this->assignReference($refId, $owningId);
     }
     foreach ($referencedCollection as $refRecord) {
         $id = $refRecord->getInternalId();
         if (!$this->isReferenced($id) && !$this->hasNullReference($id)) {
             $reference = $isOneToMany ? array() : null;
             $this->setReference($id, $reference);
         }
     }
 }
示例#2
0
 /**
  * @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;
     }
 }
示例#3
0
 /**
  * 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);
 }
示例#4
0
 /**
  * Saves record on owning relation
  *
  * @param  array                    $row
  * @param  \Dive\Relation\Relation  $relation
  * @param  string                   $value
  * @return array
  */
 private function saveRecordOnReferencedRelation(array $row, Relation $relation, $value)
 {
     $refTable = $relation->getReferencedTable();
     $relatedId = $this->getRecordIdFromMap($refTable, $value);
     if ($relatedId === false) {
         $relatedId = $this->saveRelatedRecord($refTable, $value);
     }
     $row[$relation->getOwningField()] = $relatedId;
     return $row;
 }