/** * 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; }
/** * 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; }
/** * @param array $userData */ private function givenIHaveAUserRecordWithData(array $userData) { $this->record = $this->rm->getOrCreateRecord('user', $userData); }