/** * Obtains the metadata for the named table. * * @param string $name table name * @param boolean $refresh if we need to refresh schema cache for a table. * Parameter available since 1.1.9 * * @return TableSchema table metadata. Null if the named table does not exist. */ public function getTable($name, $refresh = false) { if (!$refresh) { if (isset($this->tables[$name])) { return $this->tables[$name]; } elseif (null !== ($table = $this->connection->getFromCache('table:' . $name))) { $this->tables[$name] = $table; return $this->tables[$name]; } } if ($this->connection->tablePrefix !== null && strpos($name, '{{') !== false) { $realName = preg_replace('/\\{\\{(.*?)\\}\\}/', $this->connection->tablePrefix . '$1', $name); } else { $realName = $name; } if (null === ($table = $this->loadTable($realName))) { return null; } // merge db extras if (!empty($extras = $this->connection->getSchemaExtrasForTables($name, false))) { $extras = isset($extras[0]) ? $extras[0] : null; $table->fill($extras); } if (!empty($extras = $this->connection->getSchemaExtrasForFields($name, '*'))) { foreach ($extras as $extra) { if (!empty($columnName = isset($extra['field']) ? $extra['field'] : null)) { if (null !== ($column = $table->getColumn($columnName))) { $column->fill($extra); } } } } $this->tables[$name] = $table; $this->connection->addToCache('table:' . $name, $table, true); return $table; }
/** * Obtains the metadata for the named table. * * @param string $name table name * @param boolean $refresh if we need to refresh schema cache for a table. * Parameter available since 1.1.9 * * @return TableSchema table metadata. Null if the named table does not exist. */ public function getTable($name, $refresh = false) { if (!$refresh) { if (isset($this->tables[$name])) { return $this->tables[$name]; } elseif (null !== ($table = $this->connection->getFromCache('table:' . $name))) { $this->tables[$name] = $table; return $this->tables[$name]; } } // if ($this->connection->tablePrefix !== null && strpos($name, '{{') !== false) { // $realName = preg_replace('/\{\{(.*?)\}\}/', $this->connection->tablePrefix . '$1', $name); // } else { // $realName = $name; // } // check if know anything about this table already $ndx = strtolower($name); if (empty($this->tableNames[$ndx])) { $this->getCachedTableNames(); if (empty($this->tableNames[$ndx])) { return null; } } if (null === ($table = $this->loadTable($this->tableNames[$ndx]))) { return null; } // merge db extras if (!empty($extras = $this->connection->getSchemaExtrasForFields($name, '*'))) { foreach ($extras as $extra) { if (!empty($columnName = isset($extra['field']) ? $extra['field'] : null)) { if (null !== ($column = $table->getColumn($columnName))) { if (!$column->isForeignKey && !empty($extra['ref_table'])) { $column->fill($extra); // include additional ref info $column->isForeignKey = true; $column->isVirtualForeignKey = true; if (!empty($extra['ref_service'])) { $column->isForeignRefService = true; } // Add it to our foreign references as well $relatedInfo = array_merge(array_except($extra, ['label', 'description']), ['type' => RelationSchema::BELONGS_TO, 'is_virtual' => true, 'is_foreign_service' => $column->isForeignRefService, 'field' => $column->name]); $relation = new RelationSchema($relatedInfo); $table->addRelation($relation); } else { // Exclude potential virtual reference info $refExtraFields = ['ref_service', 'ref_service_id', 'ref_table', 'ref_fields', 'ref_on_update', 'ref_on_delete']; $column->fill(array_except($extra, $refExtraFields)); } } elseif (ColumnSchema::TYPE_VIRTUAL === (isset($extra['extra_type']) ? $extra['extra_type'] : null)) { $extra['name'] = $extra['field']; $extra['allow_null'] = true; // make sure it is not required $column = new ColumnSchema($extra); $table->addColumn($column); } } } } if (!empty($extras = $this->connection->getSchemaExtrasForFieldsReferenced($name, '*'))) { foreach ($extras as $extra) { if (!empty($columnName = isset($extra['ref_fields']) ? $extra['ref_fields'] : null)) { if (null !== ($column = $table->getColumn($columnName))) { // Add it to our foreign references as well $relatedInfo = ['type' => RelationSchema::HAS_MANY, 'field' => $column->name, 'is_virtual' => true, 'is_foreign_service' => !empty($extra['service']), 'ref_service' => empty($extra['service']) ? null : $extra['service'], 'ref_service_id' => $extra['service_id'], 'ref_table' => $extra['table'], 'ref_fields' => $extra['field']]; $relation = new RelationSchema($relatedInfo); $table->addRelation($relation); } } } } if (!empty($extras = $this->connection->getSchemaExtrasForRelated($name, '*'))) { foreach ($extras as $extra) { if (!empty($relatedName = isset($extra['relationship']) ? $extra['relationship'] : null)) { if (null !== ($relationship = $table->getRelation($relatedName))) { $relationship->fill($extra); if (isset($extra['always_fetch']) && $extra['always_fetch']) { $table->fetchRequiresRelations = true; } } } } } $this->tables[$name] = $table; $this->connection->addToCache('table:' . $name, $table, true); return $table; }