/** * 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(); }
/** * Returns the description of the failure * * The beginning of failure messages is "Failed asserting that" in most * cases. This method should return the second part of that sentence. * * @param Relation $relation Evaluated value or object. * @return string */ protected function failureDescription($relation) { $owningAlias = $relation->getOwningAlias(); $owningTable = $relation->getOwningTable(); $referencedAlias = $relation->getReferencedAlias(); $referencedTable = $relation->getReferencedTable(); return $this->toString() . "{$owningTable}->{$referencedAlias}: {$referencedTable} | {$referencedTable}->{$owningAlias}: {$owningTable}"; }
/** * adds foreign key by relation instance * * @param \Dive\Relation\Relation $relation * @return Migration * @throws MigrationException */ public function addForeignKeyByRelation(Relation $relation) { if ($relation->getOwningTable() !== $this->tableName) { throw new MigrationException("\n Relation does not belong to table {$this->tableName}, it belongs to " . $relation->getOwningTable() . '!'); } $owningField = $relation->getOwningField(); $referencedTable = $relation->getReferencedTable(); $referencedField = $relation->getReferencedField(); $onDelete = $relation->getOnDelete(); $onUpdate = $relation->getOnUpdate(); return $this->addForeignKey($owningField, $referencedTable, $referencedField, $onDelete, $onUpdate); }
/** * @param RecordManager $rm * @param Relation $relation * @return string */ protected function insertRequiredLocalRelationGraph(RecordManager $rm, Relation $relation) { $randomGenerator = new FieldValuesGenerator(); $conn = $rm->getConnection(); $refTableName = $relation->getReferencedTable(); $refTable = $rm->getTable($refTableName); $refFields = $refTable->getFields(); $data = array(); // recursion: walk through all local relations that are required and handle this by calling this method with // next relation $owningRelations = $refTable->getReferencedRelationsIndexedByOwningField(); foreach ($owningRelations as $owningField => $owningRelation) { // check if field is required (default of matchType) and insert required related data if ($randomGenerator->matchType($refFields[$owningField])) { $data[$owningField] = $this->insertRequiredLocalRelationGraph($rm, $owningRelation); } } // insert a record and return its id $data = $randomGenerator->getRandomRecordData($refFields, $data); $conn->insert($refTable, $data); return $conn->getLastInsertId($refTableName); }
/** * Saves record on owning relation * * @param array $row * @param \Dive\Relation\Relation $relation * @param string $value * @return array */ private function saveRecordOnReferencedRelation(array $row, Relation $relation, $value) { $refTable = $relation->getReferencedTable(); $relatedId = $this->getRecordIdFromMap($refTable, $value); if ($relatedId === false) { $relatedId = $this->saveRelatedRecord($refTable, $value); } $row[$relation->getOwningField()] = $relatedId; return $row; }