Ejemplo n.º 1
0
 /**
  * @param array $recordData
  */
 private function thenItShouldHaveCreatedARecordWithData(array $recordData)
 {
     $this->assertNotNull($this->resultRecord);
     $this->assertFalse($this->resultRecord->exists());
     foreach ($recordData as $fieldName => $fieldValue) {
         $this->assertEquals($fieldValue, $this->resultRecord->get($fieldName));
     }
 }
Ejemplo n.º 2
0
 /**
  * @param  Record $record
  * @return RecordInvalidException
  */
 public static function createByRecord(Record $record)
 {
     $errorStack = $record->getErrorStack();
     $message = "Record {$record} is invalid!\n";
     foreach ($errorStack as $fieldName => $errorCodes) {
         $value = $record->get($fieldName);
         $type = gettype($value);
         $valueAsString = "({$type})";
         if (is_string($value) || is_numeric($value)) {
             $valueAsString .= $value;
         } else {
             if (is_bool($value)) {
                 $valueAsString .= $value ? 'TRUE' : 'FALSE';
             }
         }
         $message .= "  {$fieldName}: {$valueAsString} [codes: " . implode(', ', $errorCodes) . "]\n";
     }
     return new self($message);
 }
Ejemplo n.º 3
0
 /**
  * @param Record $record
  * @param array  $uniqueIndexesToCheck
  * @return \Dive\Query\Query
  * @throws \Dive\Exception
  */
 private function getUniqueIndexesQuery(Record $record, array $uniqueIndexesToCheck)
 {
     $table = $record->getTable();
     $conn = $table->getConnection();
     $conditions = array();
     $queryParams = array();
     $recordExists = $record->exists();
     $identifier = array();
     if ($recordExists) {
         $condition = '';
         foreach ($record->getIdentifierFieldIndexed() as $idField => $idValue) {
             $condition .= $conn->quoteIdentifier($idField) . ' != ? AND ';
             $identifier[] = $idValue;
         }
         // strip last AND from string
         $condition = substr($condition, 0, -4);
         $conditions['primary'] = $condition;
     }
     foreach ($uniqueIndexesToCheck as $uniqueName => $uniqueIndexToCheck) {
         $isNullConstrained = $table->isUniqueIndexNullConstrained($uniqueName);
         $conditionParams = array();
         $condition = '';
         $fieldNames = $uniqueIndexToCheck['fields'];
         foreach ($fieldNames as $fieldName) {
             $fieldNameQuoted = $conn->quoteIdentifier($fieldName);
             $fieldValue = $record->get($fieldName);
             if ($fieldValue !== null) {
                 $condition .= $fieldNameQuoted . ' = ? AND ';
                 $conditionParams[] = $fieldValue;
             } else {
                 if ($isNullConstrained) {
                     $condition .= $fieldNameQuoted . ' IS NULL AND ';
                 } else {
                     throw new Exception("Cannot process unique index for creating query to check whether the record is unique, or not!");
                 }
             }
         }
         // strip last AND from string
         $condition = substr($condition, 0, -4);
         $conditions[$uniqueName] = $condition;
         $queryParams = array_merge($queryParams, $conditionParams);
     }
     $whereCondition = ($recordExists ? array_shift($conditions) . ' AND (' : '') . implode(' OR ', $conditions) . ($recordExists ? ')' : '');
     $query = $table->createQuery();
     $query->where($whereCondition);
     foreach ($conditions as $uniqueName => $condition) {
         $query->addSelect("({$condition}) AS " . $conn->quoteIdentifier($uniqueName));
     }
     $whereParams = $recordExists ? array_merge($identifier, $queryParams) : $queryParams;
     $query->setParams(Query::PART_SELECT, $queryParams);
     $query->setParams(Query::PART_WHERE, $whereParams);
     $query->limit(1);
     return $query;
 }
Ejemplo n.º 4
0
 /**
  * Gets id of old referenced record
  *
  * @param  Record $owningRecord
  * @return bool|string
  */
 private function getOldReferencedId(Record $owningRecord)
 {
     $oid = $owningRecord->getOid();
     $oldRefId = $owningRecord->get($this->relation->getOwningField());
     if (!$oldRefId && $this->hasFieldMapping($oid)) {
         $oldRefId = Record::NEW_RECORD_ID_MARK . $this->getFieldMapping($oid);
     }
     return $oldRefId;
 }
Ejemplo n.º 5
0
 /**
  * @param  Record $record
  * @param  string $fieldName
  * @return bool
  */
 protected function validateFieldLength(Record $record, $fieldName)
 {
     if ($this->isCheckDisabled(self::CODE_FIELD_LENGTH)) {
         return true;
     }
     $value = $record->get($fieldName);
     $field = $record->getTable()->getField($fieldName);
     $validator = $this->getDataTypeValidator($field['type']);
     return $validator->validateLength($value, $field);
 }
Ejemplo n.º 6
0
 /**
  * @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!");
     }
 }
Ejemplo n.º 7
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;
 }