/** * Create a list of tables the structure should be generated for. * * @return array */ protected function getTables() { // check if the tables are set // if so return the tables if ($this->tables) { return $this->tables; } // read the table argument // if table argument empty get list of all tables in db $this->initSchemaGenerator(); $tables_in_db = $this->schemaGenerator->getTables(); if ($this->option('tables')) { $tables = explode(',', $this->option('tables')); foreach ($tables as $index => $table) { if (!in_array($table, $tables_in_db)) { unset($tables[$index]); } } } else { $tables = $tables_in_db; } // return array of creatable tables $tables = $this->removeExcludedTables($tables); if (empty($tables)) { throw new NothingToGenerateException('There is nothing to generate.'); } return $tables; }
/** * Create the relationship information if it not exists allready. * * @return array */ public function relationships() { if (!is_null($this->relationships)) { return $this->relationships; } // get a list of all tables in the database $tables = $this->schemaGenerator->getTables(); $db_info = []; $rules = []; // init the table information foreach ($tables as $tableName) { $rules[$tableName] = ['hasMany' => [], 'hasOne' => [], 'belongsTo' => [], 'belongsToMany' => []]; $db_info[$tableName] = $this->getTableInformation($tableName); } foreach ($db_info as $table => $properties) { $foreign = $properties['foreign']; $primary = $properties['primary']; $isManyToMany = $this->detectManyToMany($db_info, $table); if ($isManyToMany === true) { $this->addManyToManyRules($tables, $table, $db_info, $rules); } /* * the below used to be in an ELSE clause but we should be as verbose as possible * when we detect a many-to-many table, we still want to set relations on it * else */ foreach ($foreign as $fk) { $isOneToOne = $this->detectOneToOne($fk, $primary); if ($isOneToOne) { $this->addOneToOneRules($tables, $table, $rules, $fk); } else { $this->addOneToManyRules($tables, $table, $rules, $fk); } } } // filter out the required tables $requested_tables = array_flip($this->getTables()); $this->relationships = array_intersect_key($rules, $requested_tables); }