Skip to content

Instantly share code, notes, and snippets.

@lantian
Last active December 19, 2015 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lantian/5960738 to your computer and use it in GitHub Desktop.
Save lantian/5960738 to your computer and use it in GitHub Desktop.
Phalcon createTable
<?php
foreach ($this->_getModels() as $model) {
$columns = array();
$primaries = array();
foreach ($model['columns'] as $columnName => $column) {
if (isset($column['is_primary']) && $column['is_primary']) {
$primaries = $columnName;
}
$columns[] = new Column(
$columnName,
array(
"type" => $column['type'],
"size" => (isset($column['size']) ? $column['size'] : false),
"notNull" => (isset($column['nullable']) ? $column['nullable'] : false),
(isset($column['identity']) && (isset($column['is_numeric']) && $column['is_numeric']) ? "autoIncrement" : null) => true, // error in Phalcon, when autoIncrement is false for 'string' type
"primary" => (isset($column['is_primary']) ? $column['is_primary'] : false),
"isNumeric" => (isset($column['is_numeric']) ? $column['is_numeric'] : false),
)
);
}
$connection->createTable($model['name'], null, array(
"columns" => $columns,
"indexes" => array(new Index("PRIMARY", array($primaries)))
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment