Beispiel #1
0
 /**
  * @param  Table  $table
  * @param  string $relationName
  * @return bool|\Dive\Relation\Relation
  */
 private function getOneToOneRelation(Table $table, $relationName)
 {
     if ($relationName && $table->hasRelation($relationName)) {
         $relation = $table->getRelation($relationName);
         if ($relation->isOneToOne()) {
             return $relation;
         }
     }
     return false;
 }
Beispiel #2
0
 /**
  * Gets record (retrieved from repository if exists, or create new record!)
  *
  * @param  Table $table
  * @param  array $data
  * @param  bool  $exists
  * @throws \UnexpectedValueException
  * @return Record
  */
 public function getOrCreateRecord(Table $table, array $data, $exists = false)
 {
     $id = $table->getIdentifierAsString($data);
     if ($id !== false && $table->isInRepository($id)) {
         $record = $table->getFromRepository($id);
     } else {
         $record = $table->createRecord($data, $exists);
     }
     return $record;
 }
Beispiel #3
0
 private function thenSaveShouldFail()
 {
     $e = null;
     try {
         $rm = $this->record->getRecordManager();
         $rm->scheduleSave($this->record);
         $rm->commit();
     } catch (Exception $e) {
         // see assert
     }
     $this->assertNotNull($e, 'record was saved: ' . print_r($this->table->createQuery()->fetchArray(), true));
 }
Beispiel #4
0
 /**
  * @param  mixed $record
  * @throws CollectionException
  */
 protected function throwExceptionIfRecordDoesNotMatchTable($record)
 {
     if (!$record instanceof Record) {
         throw new CollectionException('Argument #1 must be an instance of \\Dive\\Record!' . 'You gave me: ' . (is_object($record) ? get_class($record) : gettype($record)));
     }
     if ($record->getTable() != $this->table) {
         throw new CollectionException('Add record does not match table instance!' . ' Expected: ' . $this->table->getTableName() . ' You gave me: ' . $record->getTable()->getTableName());
     }
 }
Beispiel #5
0
 public function testRecordReturnsToBeUnmodified()
 {
     $initialData = array('username' => 'David');
     $record = $this->table->createRecord($initialData);
     $record->set('username', 'Micheal');
     $this->assertTrue($record->isModified());
     $record->set('username', 'David');
     $this->assertFalse($record->isModified());
 }
Beispiel #6
0
 /**
  * @param array $references
  */
 public function loadReferences(array $references)
 {
     foreach ($references as $relationName => $relatedReferences) {
         /** @var RecordCollection|Record[]|Record $related */
         $related = $this->_table->getRelation($relationName)->getReferenceFor($this, $relationName);
         if (is_array($relatedReferences)) {
             if ($related instanceof RecordCollection) {
                 foreach ($related as $relatedRecord) {
                     $relatedRecord->loadReferences($relatedReferences);
                 }
             } else {
                 if ($related instanceof Record) {
                     $related->loadReferences($relatedReferences);
                 }
             }
         }
     }
 }
Beispiel #7
0
 /**
  * hydrates record
  *
  * @param   Table $table
  * @param   array $row
  * @return  Record
  */
 protected function hydrateRecord(Table $table, array $row)
 {
     $record = $this->recordManager->getOrCreateRecord($table->getTableName(), $row, true);
     return $record;
 }
 private function whenIFindOrCreateByPrimaryKey()
 {
     $this->resultRecord = $this->table->findOrCreateRecord($this->storedRecord->getIdentifierFieldIndexed());
 }
Beispiel #9
0
 /**
  * Inserts new data record to database
  *
  * @param  \Dive\Table      $table
  * @param  array            $data
  * @return string
  */
 protected static function insertDataset(Table $table, array $data)
 {
     $conn = $table->getConnection();
     $affectedRows = $conn->insert($table, $data);
     return $affectedRows == 1 ? $conn->getLastInsertId() : false;
 }
Beispiel #10
0
 /**
  * Clear dataset ids by given table
  *
  * @param \Dive\Table $table
  */
 public function clearByTable(Table $table)
 {
     $connIndex = $this->getConnectionIndex($table->getConnection());
     if ($connIndex === false) {
         return;
     }
     $tableName = $table->getTableName();
     unset($this->tables[$connIndex][$tableName]);
     unset($this->registry[$connIndex][$tableName]);
 }
Beispiel #11
0
 public function testClear()
 {
     $this->table->createRecord(array('id' => 7, 'username' => 'Bart'), true);
     $this->repository->clear();
     $this->assertEquals(0, $this->repository->count());
 }
Beispiel #12
0
 /**
  * @param  Table $table
  * @param  array $identifier
  * @throws InvalidArgumentException
  */
 private static function throwExceptionIfIdentifierDoesNotMatchTableIdentifier(Table $table, array $identifier)
 {
     $identifierFields = $table->getIdentifierFields();
     if (count($identifierFields) != count($identifier)) {
         throw new InvalidArgumentException("Identifier '" . implode(',', $identifier) . "'" . ' does not match table identifier (' . implode(', ', $identifierFields) . ')');
     }
 }
Beispiel #13
0
 /**
  * @param Table $table
  * @param array $row
  * @return array
  */
 private function saveRequiredRelations(Table $table, array $row)
 {
     foreach ($table->getRelations() as $relationName => $relation) {
         $owning = $relation->getOwningField();
         if (!$relation->isReferencedSide($relationName) || isset($row[$relationName]) || isset($row[$owning])) {
             continue;
         }
         if ($table->isFieldRequired($owning)) {
             $row[$owning] = $this->saveRelatedRecord($relation->getReferencedTable(), null, array());
         }
     }
     return $row;
 }