method getCreate documentation in bbn\Db\Languages\Mysql
function(string $table, array $model = NULL)
{
$st = '';
if (!$model) {
$model = $this->modelize($table);
}
if ($st = $this->getCreateTable($table, $model)) {
if (empty($model['keys'])) {
return $st;
}
$lines = X::split($st, PHP_EOL);
$end = array_pop($lines);
$st = X::join($lines, PHP_EOL);
foreach ($model['keys'] as $name => $key) {
$st .= ',' . PHP_EOL . ' ';
if (
!empty($key['unique']) &&
(count($key['columns']) === 1) &&
isset($model['fields'][$key['columns'][0]]) &&
isset($model['fields'][$key['columns'][0]]['key']) &&
$model['fields'][$key['columns'][0]]['key'] === 'PRI'
) {
$st .= 'PRIMARY KEY';
} elseif (!empty($key['unique'])) {
$st .= 'UNIQUE KEY ' . $this->escape($name);
} else {
$st .= 'KEY ' . $this->escape($name);
}
$st .= ' (' . implode(
',', array_map(
function ($a) {
return $this->escape($a);
}, $key['columns']
)
) . ')';
}
// For avoiding constraint names conflicts
$keybase = strtolower(Str::genpwd(8, 4));
$i = 1;
foreach ($model['keys'] as $name => $key) {
if (!empty($key['ref_table']) && !empty($key['ref_column'])) {
$st .= ',' . PHP_EOL . ' ' .
'CONSTRAINT ' . $this->escape($keybase . $i) . ' FOREIGN KEY (' . $this->escape($key['columns'][0]) . ') ' .
'REFERENCES ' . $this->escape($key['ref_table']) . ' (' . $this->escape($key['ref_column']) . ')' .
(!empty($key['delete']) ? ' ON DELETE ' . $key['delete'] : '') .
(!empty($key['update']) ? ' ON UPDATE ' . $key['update'] : '');
$i++;
}
}
$st .= PHP_EOL . $end;
}
return $st;
}
BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions