/** * Builds the relationships * @return void */ private function buildRelationships() { /** @var Relationship[] $relationships */ $relationships = array(); $dbCompareTable = new Table($this->getConfiguration()->getConnection(), $this->getTableName()); foreach ($dbCompareTable->getConstraints() as $constraint) { $relationship = new Relationship($this->getConfiguration(), $this->getTableName(), implode(', ', $constraint->getLocalColumns()), $constraint->getRemoteTable(), implode(', ', $constraint->getRemoteColumns()), 'belongsTo', 'ON DELETE ' . $constraint->getDeleteAction() . ' ON UPDATE ' . $constraint->getUpdateAction()); $this->getAbstractClass()->addUse('Phalcon\\Mvc\\Model\\Relation'); $this->getAbstractClass()->addUse($relationship->getRemoteModel() . ' as Child' . $relationship->getRemoteShortModel()); $relationships[] = $relationship; } //Get One to Many relationships $database = $this->getCurrentDatabaseName(); $sql = 'SELECT DISTINCT(TABLE_NAME) as tableName FROM information_schema.`KEY_COLUMN_USAGE` WHERE REFERENCED_TABLE_NAME="' . $this->getTableName() . '" AND REFERENCED_TABLE_SCHEMA="' . $database . '"'; /** @var ResultSet $results */ $foreignKeys = $this->getConfiguration()->getConnection()->query($sql); while ($foreignKey = $foreignKeys->fetch()) { $foreignKey = (object) $foreignKey; $tableName = $foreignKey->tableName; $dbCompareTable = new Table($this->getConfiguration()->getConnection(), $tableName); foreach ($dbCompareTable->getConstraints() as $constraint) { if ($constraint->getRemoteTable() == $this->getTableName()) { if (implode(', ', $constraint->getRemoteColumns()) === 'venueId`, `userID') { exit; } $relationship = new Relationship($this->getConfiguration(), $this->getTableName(), implode(', ', $constraint->getRemoteColumns()), $tableName, implode(', ', $constraint->getLocalColumns()), 'hasMany', 'ON DELETE ' . $constraint->getDeleteAction() . ' ON UPDATE ' . $constraint->getUpdateAction()); $this->getAbstractClass()->addUse('Phalcon\\Mvc\\Model\\Relation'); $this->getAbstractClass()->addUse($relationship->getRemoteModel() . ' as Child' . $relationship->getRemoteShortModel()); $this->getAbstractClass()->addUse('Phalcon\\Mvc\\Model\\Relation'); $this->getAbstractClass()->addUse('Phalcon\\Mvc\\Model\\Resultset\\Simple as ResultSet'); $this->getAbstractClass()->addUse($relationship->getRemoteModel() . ' as Child' . $relationship->getRemoteShortModel()); $relationships[] = $relationship; } } } foreach ($relationships as $relationship) { $relationship->setRelationships($relationships); } $this->setRelationships($relationships); }
/** * Compares constraints * * @param Table $table * @param string $prefix * * @return string[] * @throws \Exception */ private function equalsConstraints(Table $table, $prefix) { $thisConstraints = $this->getConstraints(); $tableConstraints = $table->getConstraints(); $errors = []; foreach ($thisConstraints as $constraint) { if (array_key_exists($constraint->getName(), $tableConstraints)) { $errors = array_merge($errors, $constraint->equals($tableConstraints[$constraint->getName()], $prefix)); } } $thisConstraintNames = array_keys($thisConstraints); $tableConstraintNames = array_keys($tableConstraints); $diff = array_diff($tableConstraintNames, $thisConstraintNames); if (!empty($diff)) { $errors[] = $prefix . 'Extra Constraints: ' . "\n" . implode("\n", $diff); } $diff = array_diff($thisConstraintNames, $tableConstraintNames); if (!empty($diff)) { $errors[] = $prefix . 'Missing Constraints: ' . "\n" . implode("\n", $diff); } return $errors; }
/** * Builds a table * @param string[] $ignoreTables * @return void */ private function buildDatabase($ignoreTables) { $tables = $this->getDbAdapter()->query('SHOW FULL TABLES WHERE Table_Type!="VIEW"')->fetchAll(); array_walk($tables, function (&$item) { $item = $item[0]; }); $tables = array_diff($tables, $ignoreTables); $tableObjects = []; foreach ($tables as $table) { $tableObject = new Table($this->getDbAdapter(), $table); $tableObjects[$tableObject->getName()] = $tableObject; } $this->setTables($tableObjects); }