Ejemplo n.º 1
0
 /**
  * @param  Record $record
  * @param  string $uniqueName
  * @return bool
  * @throws \Dive\Table\TableException
  */
 private function isCheckRequired(Record $record, $uniqueName)
 {
     $table = $record->getTable();
     $uniqueIndex = $table->getIndex($uniqueName);
     $isNullConstrained = $table->isUniqueIndexNullConstrained($uniqueName);
     $uniqueFields = $uniqueIndex['fields'];
     $checkIsRequired = false;
     foreach ($uniqueFields as $fieldName) {
         if ($record->exists() && !$record->isFieldModified($fieldName)) {
             continue;
         }
         if ($isNullConstrained) {
             return true;
         }
         if ($record->get($fieldName) === null) {
             return false;
         }
         $checkIsRequired = true;
     }
     return $checkIsRequired;
 }
Ejemplo n.º 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;
     }
 }
Ejemplo n.º 3
0
 /**
  * @param Record $record
  * @param string $relationName
  * @return array
  * @throws RelationException
  */
 public function getOriginalReferencedIds(Record $record, $relationName)
 {
     $isReferencedSide = $this->isReferencedSide($relationName);
     if ($isReferencedSide) {
         $owningField = $this->getOwningField();
         if ($record->isFieldModified($owningField)) {
             return array($record->getModifiedFieldValue($owningField));
         }
         return array($record->get($owningField));
     }
     $query = $this->getReferenceQuery($record, $relationName, array($record->getInternalId()));
     $originalReferencedIds = $query->fetchScalars();
     if ($this->isOneToOne()) {
         $moreThanOne = isset($originalReferencedIds[1]);
         if ($moreThanOne) {
             throw new RelationException("One-to-one relation has returned more than one result!");
         }
     }
     return $originalReferencedIds;
 }