Ejemplo n.º 1
0
 /**
  * Returns a string representation of the object.
  *
  * @return string
  */
 public function toString()
 {
     $oid = $this->record->getOid();
     $recordTableName = $this->record->getTable()->getTableName();
     $relationName = $this->relationName;
     return "{$recordTableName}[{$oid}]->{$relationName} has field mapping";
 }
 /**
  * Returns a string representation of the object.
  *
  * @return string
  */
 public function toString()
 {
     $id = $this->record->getInternalId();
     $recordTableName = $this->record->getTable()->getTableName();
     $relationName = $this->relationName;
     return "{$recordTableName}[{$id}]->{$relationName} has reference";
 }
Ejemplo n.º 3
0
 /**
  * @param Record $other
  * @return string
  */
 protected function failureDescription($other)
 {
     $tableName = $other->getTable()->getTableName();
     return 'Record ' . $tableName . ' ' . $this->toString();
 }
Ejemplo n.º 4
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.º 5
0
 /**
  * Get relation referenced repository
  *
  * @param  Record $record
  * @param  string $relationName
  * @return \Dive\Table\Repository
  */
 protected function getRefRepository(Record $record, $relationName)
 {
     $rm = $record->getTable()->getRecordManager();
     $tableName = $this->relation->isReferencedSide($relationName) ? $this->relation->getReferencedTable() : $this->relation->getOwningTable();
     $refTable = $rm->getTable($tableName);
     return $refTable->getRepository();
 }
Ejemplo n.º 6
0
 /**
  * @param Record $record
  */
 private function assertIdentifierUpdatedInRepository(Record $record)
 {
     $repository = $record->getTable()->getRepository();
     $internalId = $record->getInternalId();
     $this->assertStringStartsNotWith('_', $internalId[0]);
     $this->assertTrue($repository->hasByInternalId($internalId));
 }
Ejemplo n.º 7
0
 /**
  * @param  Record $record
  * @param  array  $visited
  * @return array
  */
 private function getLoadedReferences(Record $record, array $visited = array())
 {
     $oid = $record->getOid();
     if (in_array($oid, $visited)) {
         return false;
     }
     $visited[] = $oid;
     $actualReferences = array();
     $table = $record->getTable();
     $relations = $table->getRelations();
     foreach ($relations as $relationName => $relation) {
         if ($relation->hasReferenceLoadedFor($record, $relationName)) {
             $related = $relation->getReferenceFor($record, $relationName);
             if ($related instanceof RecordCollection) {
                 foreach ($related as $relatedRecord) {
                     $loadedReferences = $this->getLoadedReferences($relatedRecord, $visited);
                     if ($loadedReferences !== false) {
                         $actualReferences[$relationName] = $loadedReferences;
                     }
                 }
             } else {
                 if ($related instanceof Record) {
                     $loadedReferences = $this->getLoadedReferences($related, $visited);
                     if ($loadedReferences !== false) {
                         $actualReferences[$relationName] = $loadedReferences;
                     }
                 }
             }
         }
     }
     return $actualReferences;
 }
Ejemplo n.º 8
0
 /**
  * @param  Record $record
  * @return array
  */
 private function getFieldNamesForValidation(Record $record)
 {
     if ($record->exists()) {
         $fields = $record->getModifiedFields();
     } else {
         $table = $record->getTable();
         $fields = $table->getFields();
         if ($table->hasAutoIncrementTrigger()) {
             $idFields = $table->getIdentifierFields();
             foreach ($idFields as $idFieldName) {
                 unset($fields[$idFieldName]);
             }
         }
     }
     return array_keys($fields);
 }
Ejemplo n.º 9
0
 /**
  * @param Record $record
  * @param string $id
  */
 private function setForeignKeyFieldOfRelatedRecords(Record $record, $id)
 {
     $table = $record->getTable();
     $owningRelations = $table->getOwningRelations();
     foreach ($owningRelations as $relationName => $relation) {
         $owningField = $relation->getOwningField();
         /** @var Record|Record[] $related */
         $related = $relation->getReferenceFor($record, $relationName);
         if ($related instanceof RecordCollection) {
             foreach ($related as $relatedRecord) {
                 $relatedRecord->set($owningField, $id);
             }
         } else {
             if ($related instanceof Record) {
                 $related->set($owningField, $id);
             }
         }
     }
 }
Ejemplo n.º 10
0
 /**
  * @param string      $relationName
  * @param Record|null $reference
  * @throws RelationException
  */
 private function throwReferenceMustBeRecordCollectionException($relationName, $reference)
 {
     if (!$reference instanceof RecordCollection) {
         throw new RelationException("Reference for relation '{$relationName}' must be an instance of \\Dive\\Collection\\RecordCollection!");
     }
     $joinTableName = $this->getJoinTableName($relationName);
     if ($reference->getTable()->getTableName() !== $joinTableName) {
         throw new RelationException("RecordCollection for relation '{$relationName}' must be a collection for table '{$joinTableName}'!");
     }
 }
Ejemplo n.º 11
0
 /**
  * @param Record $record
  */
 private function assertRecordIsNotReferenced(Record $record)
 {
     $internalId = $record->getInternalId();
     $table = $record->getTable();
     $relations = $table->getRelations();
     $message = "Record with id '{$internalId}' in table '" . $table->getTableName() . "'";
     foreach ($relations as $relationName => $relation) {
         $this->assertRecordIsNotReferencedByRelation($record, $relationName, $message);
     }
 }
 /**
  * @return Relation
  */
 private function getRelation()
 {
     $relationName = CamelCase::toCamelCase($this->relatedRecord->getTable()->getTableName());
     return $this->storedRecord->getTableRelation($relationName);
 }