Cake\Database\Connection::schemaCollection PHP Method

schemaCollection() public method

Gets or sets a Schema\Collection object for this connection.
public schemaCollection ( Cake\Database\Schema\Collection $collection = null ) : Cake\Database\Schema\Collection
$collection Cake\Database\Schema\Collection The schema collection object
return Cake\Database\Schema\Collection
    public function schemaCollection(SchemaCollection $collection = null)
    {
        if ($collection !== null) {
            return $this->_schemaCollection = $collection;
        }
        if ($this->_schemaCollection !== null) {
            return $this->_schemaCollection;
        }
        if (!empty($this->_config['cacheMetadata'])) {
            return $this->_schemaCollection = new CachedCollection($this, $this->_config['cacheMetadata']);
        }
        return $this->_schemaCollection = new SchemaCollection($this);
    }

Usage Example

 /**
  * Checks whether connected database is empty or not.
  *
  * @param \Cake\Database\Connection $conn Database connection to use
  * @return bool True if database if empty and tables can be imported, false if
  *  there are some existing tables
  */
 public function isDbEmpty($conn)
 {
     $Folder = new Folder($this->config('schemaPath'));
     $existingSchemas = $conn->schemaCollection()->listTables();
     $newSchemas = array_map(function ($item) {
         return Inflector::underscore(str_replace('Schema.php', '', $item));
     }, $Folder->read()[1]);
     $result = !array_intersect($existingSchemas, $newSchemas);
     if (!$result) {
         $this->error(__d('installer', 'A previous installation of QuickAppsCMS already exists, please drop your database tables before continue.'));
     }
     return $result;
 }