yii\db\Connection::getSchema PHP Method

getSchema() public method

Returns the schema information for the database opened by this connection.
public getSchema ( ) : Schema
return Schema the schema information for the database opened by this connection.
    public function getSchema()
    {
        if ($this->_schema !== null) {
            return $this->_schema;
        } else {
            $driver = $this->getDriverName();
            if (isset($this->schemaMap[$driver])) {
                $config = !is_array($this->schemaMap[$driver]) ? ['class' => $this->schemaMap[$driver]] : $this->schemaMap[$driver];
                $config['db'] = $this;
                return $this->_schema = Yii::createObject($config);
            } else {
                throw new NotSupportedException("Connection does not support reading schema information for '{$driver}' DBMS.");
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Important - modelClass must have a public method "attributeTypes"
  * Example:
  *
  * public function attributeTypes()
  * {
  *      return [
  *          'id' => Schema::TYPE_PK,
  *          'owner_id' => Schema::TYPE_INTEGER,
  *          'name' => Schema::TYPE_STRING,
  *          'description' => Schema::TYPE_TEXT,
  *          'status' => Schema::TYPE_SMALLINT,
  *          'updated_at' => Schema::TYPE_TIMESTAMP,
  *          'created_at' => Schema::TYPE_DATETIME,
  *      ];
  * }
  *
  * Example of use:
  *
  * (new DbSync([
  *     'common\models\', // if namespace equivalent to path
  *     'Path to directory' => 'someName\models\',
  * ]))->run();
  *
  *
  * @param array $nameSpaces
  */
 public function __construct(array $nameSpaces)
 {
     foreach ($nameSpaces as $key => $nameSpace) {
         $this->nameSpaces[$key] = trim($nameSpace, '\\') . '\\';
     }
     $this->db = \Yii::$app->getDb();
     $this->tableNames = $this->db->getSchema()->getTableNames();
 }
All Usage Examples Of yii\db\Connection::getSchema