/** * @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; }
/** * @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); } } } } }
/** * Saves record * * @param Table $table * @param array $row * @param string $key * @return string */ private function saveRecord(Table $table, array $row, $key) { $tableName = $table->getTableName(); if ($key && $this->isInRecordMap($tableName, $key)) { return $this->getRecordIdFromMap($tableName, $key); } // keep foreign key relations in array to process after record has been saved $owningRelations = array(); $beforeSaveGeneratedRecords = $this->generatedRecords; $this->generatedRecords = array(); foreach ($row as $relationName => $value) { if ($table->hasRelation($relationName)) { $relation = $table->getRelation($relationName); if ($relation->isReferencedSide($relationName)) { $row = $this->saveRecordOnReferencedRelation($row, $relation, $value); } else { $owningRelations[$relationName] = array('related' => $value, 'relation' => $relation); } unset($row[$relationName]); } } $row = $this->saveRequiredRelations($table, $row); // save record $row = $this->fieldValueGenerator->getRandomRecordData($table->getFields(), $row); $record = $this->rm->getOrCreateRecord($tableName, $row); $this->rm->scheduleSave($record); $this->rm->commit(); // keep record identifier in the record map $id = $record->getIdentifierAsString(); if ($key !== null) { $this->recordAliasIdMap[$tableName][$key] = $id; } $this->generatedRecords = array_merge($beforeSaveGeneratedRecords, array(array('tableName' => $tableName, 'id' => $id)), $this->generatedRecords); // save owning relations foreach ($owningRelations as $relationData) { /** @var $relation Relation */ $relation = $relationData['relation']; $relatedRows = $relationData['related']; $this->saveRecordsOnOwningRelation($relation, $relatedRows, $id); } return $id; }