Commit 23767304 by Qiang Xue

better check of existence of tables.

parent cbf642d9
...@@ -275,10 +275,16 @@ class ActiveRecord extends Model ...@@ -275,10 +275,16 @@ class ActiveRecord extends Model
/** /**
* Returns the schema information of the DB table associated with this AR class. * Returns the schema information of the DB table associated with this AR class.
* @return TableSchema the schema information of the DB table associated with this AR class. * @return TableSchema the schema information of the DB table associated with this AR class.
* @throws InvalidConfigException if the table for the AR class does not exist.
*/ */
public static function getTableSchema() public static function getTableSchema()
{ {
return static::getDb()->getTableSchema(static::tableName()); $schema = static::getDb()->getTableSchema(static::tableName());
if ($schema !== null) {
return $schema;
} else {
throw new InvalidConfigException("The table does not exist: " . static::tableName());
}
} }
/** /**
......
...@@ -181,12 +181,12 @@ class Schema extends \yii\db\Schema ...@@ -181,12 +181,12 @@ class Schema extends \yii\db\Schema
*/ */
protected function findColumns($table) protected function findColumns($table)
{ {
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name); $rows = $this->db->createCommand("SHOW TABLES LIKE " . $this->quoteValue($table->name))->queryAll();
try { if (count($rows) === 0) {
$columns = $this->db->createCommand($sql)->queryAll();
} catch (\Exception $e) {
return false; return false;
} }
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
$columns = $this->db->createCommand($sql)->queryAll();
foreach ($columns as $info) { foreach ($columns as $info) {
$column = $this->loadColumnSchema($info); $column = $this->loadColumnSchema($info);
$table->columns[$column->name] = $column; $table->columns[$column->name] = $column;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment