DboSource::buildColumn PHP Method

buildColumn() public method

Generate a database-native column schema string
public buildColumn ( array $column ) : string
$column array An array structured like the following: array('name' => 'value', 'type' => 'value'[, options]), where options can be 'default', 'length', or 'key'.
return string
    public function buildColumn($column)
    {
        $name = $type = null;
        extract(array_merge(array('null' => true), $column));
        if (empty($name) || empty($type)) {
            trigger_error(__d('cake_dev', 'Column name or type not defined in schema'), E_USER_WARNING);
            return null;
        }
        if (!isset($this->columns[$type])) {
            trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING);
            return null;
        }
        $real = $this->columns[$type];
        $out = $this->name($name) . ' ' . $real['name'];
        if (isset($column['length'])) {
            $length = $column['length'];
        } elseif (isset($column['limit'])) {
            $length = $column['limit'];
        } elseif (isset($real['length'])) {
            $length = $real['length'];
        } elseif (isset($real['limit'])) {
            $length = $real['limit'];
        }
        if (isset($length)) {
            $out .= '(' . $length . ')';
        }
        if (($column['type'] === 'integer' || $column['type'] === 'float') && isset($column['default']) && $column['default'] === '') {
            $column['default'] = null;
        }
        $out = $this->_buildFieldParameters($out, $column, 'beforeDefault');
        if (isset($column['key']) && $column['key'] === 'primary' && ($type === 'integer' || $type === 'biginteger')) {
            $out .= ' ' . $this->columns['primary_key']['name'];
        } elseif (isset($column['key']) && $column['key'] === 'primary') {
            $out .= ' NOT NULL';
        } elseif (isset($column['default']) && isset($column['null']) && $column['null'] === false) {
            $out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL';
        } elseif (isset($column['default'])) {
            $out .= ' DEFAULT ' . $this->value($column['default'], $type);
        } elseif ($type !== 'timestamp' && !empty($column['null'])) {
            $out .= ' DEFAULT NULL';
        } elseif ($type === 'timestamp' && !empty($column['null'])) {
            $out .= ' NULL';
        } elseif (isset($column['null']) && $column['null'] === false) {
            $out .= ' NOT NULL';
        }
        if ($type === 'timestamp' && isset($column['default']) && strtolower($column['default']) === 'current_timestamp') {
            $out = str_replace(array("'CURRENT_TIMESTAMP'", "'current_timestamp'"), 'CURRENT_TIMESTAMP', $out);
        }
        return $this->_buildFieldParameters($out, $column, 'afterDefault');
    }

Usage Example

Example #1
0
 /**
  * test building columns with SQLite
  *
  * @return void
  */
 public function testBuildColumn()
 {
     $data = array('name' => 'int_field', 'type' => 'integer', 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"int_field" integer NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'name', 'type' => 'string', 'length' => 20, 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"name" varchar(20) NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'testName', 'type' => 'string', 'length' => 20, 'default' => null, 'null' => true, 'collate' => 'NOCASE');
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'testName', 'type' => 'string', 'length' => 20, 'default' => 'test-value', 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'testName', 'type' => 'integer', 'length' => 10, 'default' => 10, 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'testName', 'type' => 'integer', 'length' => 10, 'default' => 10, 'null' => false, 'collate' => 'BADVALUE');
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'huge', 'type' => 'biginteger', 'length' => 20, 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"huge" bigint(20) NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'id', 'type' => 'biginteger', 'length' => 20, 'null' => false, 'key' => 'primary');
     $result = $this->Dbo->buildColumn($data);
     $expected = '"id" bigint(20) NOT NULL PRIMARY KEY';
     $this->assertEquals($expected, $result);
 }
All Usage Examples Of DboSource::buildColumn
DboSource