src/Charcoal/Source/DatabaseSource.php
public function alterTable()
    {
        if ($this->tableExists() === false) {
            return false;
        }

        $dbh    = $this->db();
        $table  = $this->table();
        $fields = $this->getModelFields($this->model());
        $cols   = $this->tableStructure();
        foreach ($fields as $field) {
            $ident = $field->ident();

            if (!array_key_exists($ident, $cols)) {
                // The key does not exist at all.
                $query = 'ALTER TABLE `'.$table.'` ADD '.$field->sql();
                $this->logger->debug($query);
                $dbh->query($query);
            } else {
                // The key exists. Validate.
                $col   = $cols[$ident];
                $alter = true;
                if (strtolower($col['Type']) !== strtolower($field->sqlType())) {
                    $alter = true;
                }

                if ((strtolower($col['Null']) === 'no') && !$field->allowNull()) {
                    $alter = true;
                }

                if ((strtolower($col['Null']) !== 'no') && $field->allowNull()) {
                    $alter = true;
                }

                if ($col['Default'] !== $field->defaultVal()) {
                    $alter = true;
                }

                if ($alter === true) {
                    $query = 'ALTER TABLE `'.$table.'` CHANGE `'.$ident.'` '.$field->sql();
                    $this->logger->debug($query);
                    $dbh->query($query);
                }
            }
        }

        return true;
    }
public function alterTable()
    {
        if ($this->tableExists() === false) {
            return false;
        }

        $dbh    = $this->db();
        $table  = $this->table();
        $fields = $this->getModelFields($this->model());
        $cols   = $this->tableStructure();
        foreach ($fields as $field) {
            $ident = $field->ident();

            if (!array_key_exists($ident, $cols)) {
                // The key does not exist at all.
                $query = 'ALTER TABLE `'.$table.'` ADD '.$field->sql();
                $this->logger->debug($query);
                $dbh->query($query);
            } else {
                // The key exists. Validate.
                $col   = $cols[$ident];
                $alter = true;
                if (strtolower($col['Type']) !== strtolower($field->sqlType())) {
                    $alter = true;
                }

                if ((strtolower($col['Null']) === 'no') && !$field->allowNull()) {
                    $alter = true;
                }

                if ((strtolower($col['Null']) !== 'no') && $field->allowNull()) {
                    $alter = true;
                }

                if ($col['Default'] !== $field->defaultVal()) {
                    $alter = true;
                }

                if ($alter === true) {
                    $query = 'ALTER TABLE `'.$table.'` CHANGE `'.$ident.'` '.$field->sql();
                    $this->logger->debug($query);
                    $dbh->query($query);
                }
            }
        }

        return true;
    }
public function alterTable ( ) { if ( $ this -> tableExists ( ) === false ) { return false ; } $ dbh = $ this -> db ( ) ; $ table = $ this -> table ( ) ; $ fields = $ this -> getModelFields ( $ this -> model ( ) ) ; $ cols = $ this -> tableStructure ( ) ; foreach ( $ fields as $ field ) { $ ident = $ field -> ident ( ) ; if ( ! array_key_exists ( $ ident , $ cols ) ) { // The key does not exist at all. $ query = 'ALTER TABLE `' . $ table . '` ADD ' . $ field -> sql ( ) ; $ this -> logger -> debug ( $ query ) ; $ dbh -> query ( $ query ) ; } else { // The key exists. Validate. $ col = $ cols [ $ ident ] ; $ alter = true ; if ( strtolower ( $ col [ 'Type' ] ) !== strtolower ( $ field -> sqlType ( ) ) ) { $ alter = true ; } if ( ( strtolower ( $ col [ 'Null' ] ) === 'no' ) && ! $ field -> allowNull ( ) ) { $ alter = true ; } if ( ( strtolower ( $ col [ 'Null' ] ) !== 'no' ) && $ field -> allowNull ( ) ) { $ alter = true ; } if ( $ col [ 'Default' ] !== $ field -> defaultVal ( ) ) { $ alter = true ; } if ( $ alter === true ) { $ query = 'ALTER TABLE `' . $ table . '` CHANGE `' . $ ident . '` ' . $ field -> sql ( ) ; $ this -> logger -> debug ( $ query ) ; $ dbh -> query ( $ query ) ; } } } return true ; }
Alter an existing table to match the model's metadata.

@return boolean TRUE if the table was altered, otherwise FALSE.
Alter an existing table to match the model s metadata .