Browse Source

better check of existence of tables.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
2376730452
  1. 8
      framework/yii/db/ActiveRecord.php
  2. 8
      framework/yii/db/mysql/Schema.php

8
framework/yii/db/ActiveRecord.php

@ -275,10 +275,16 @@ class ActiveRecord extends Model
/**
* 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.
* @throws InvalidConfigException if the table for the AR class does not exist.
*/
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());
}
}
/**

8
framework/yii/db/mysql/Schema.php

@ -181,12 +181,12 @@ class Schema extends \yii\db\Schema
*/
protected function findColumns($table)
{
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
try {
$columns = $this->db->createCommand($sql)->queryAll();
} catch (\Exception $e) {
$rows = $this->db->createCommand("SHOW TABLES LIKE " . $this->quoteValue($table->name))->queryAll();
if (count($rows) === 0) {
return false;
}
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
$columns = $this->db->createCommand($sql)->queryAll();
foreach ($columns as $info) {
$column = $this->loadColumnSchema($info);
$table->columns[$column->name] = $column;

Loading…
Cancel
Save