method getAlterTable documentation in bbn\Db\Languages\Mysql
Return a string for alter table sql statement.
Example
$cfg = [
'fields' => [
'id' => [
'type' => 'binary',
'maxlength' => 32
],
'name' => [
'type' => 'varchar',
'maxlength' => 255,
'alter_type' => 'modify',
'new_name' => 'username',
'after' => 'id'
],
'balance' => [
'type' => 'decimal',
'maxlength' => 10,
'decimals' => 2,
'null' => true,
'default' => 0
'alter_type' => 'modify',
'after' => 'id'
],
'role_id' => [
'alter_type' => 'drop'
]
]
];
X::dump($db->getAlterTable('users', $cfg);
// (string) ALTER TABLE `users`
// ADD `id` binary(32) NOT NULL,
// CHANGE COLUMN `name` `username` varchar(255) NOT NULL AFTER `id`,
// MODIFY `balance` decimal(10,2) UNSIGNED DEFAULT 0 AFTER `id`,
// DROP COLUMN `role_id`
function(string $table, array $cfg)
{
if (empty($cfg['fields'])) {
throw new Exception(X::_('Fields are not specified'));
}
if ($this->check() && Str::checkName($table)) {
$st = 'ALTER TABLE ' . $this->escape($table) . PHP_EOL;
$done = false;
foreach ($cfg['fields'] as $name => $col) {
if (!$done) {
$done = true;
} else {
$st .= ',' . PHP_EOL;
}
$st .= $this->getAlterColumn($table, array_merge($col, [
'col_name' => $name,
'no_table_exp' => true
]));
}
}
return $st ?? '';
}
Return a string for alter table sql statement. 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