Skip to content

Instantly share code, notes, and snippets.

@nojimage
Last active October 30, 2019 03:15
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 nojimage/a71e9c1d47e0e76d6dc79fbec721d33a to your computer and use it in GitHub Desktop.
Save nojimage/a71e9c1d47e0e76d6dc79fbec721d33a to your computer and use it in GitHub Desktop.
Bake Entity with column comment
<?php
// in bootstrap_cli.php
if (PHP_SAPI === 'cli' && Configure::read('debug')) {
// Bake書き換え
EventManager::instance()->on('Bake.beforeRender', static function (Event $event) {
$view = $event->getSubject();
/* @var $view View */
// for Bake Entity
if ($view->get('propertySchema') && $view->get('table') && $view->get('name')) {
// propertySchemaを書き換えてスキーマコメントを付与する
$tableName = Inflector::pluralize($view->get('name'));
$tableLocator = TableRegistry::getTableLocator();
$config = $tableLocator->exists($tableName) ? [] : [
'table' => $view->get('table'),
'connectionName' => $view->get('connection'),
];
$model = $tableLocator->get($tableName, $config);
$schema = $model->getSchema();
$properties = $view->get('propertySchema');
foreach ($schema->columns() as $column) {
if (isset($properties[$column]) && $columnOptions = $schema->getColumn($column)) {
$newColumn = implode(' ', array_filter([$column, trim($columnOptions['comment'])]));
if ($newColumn !== $column) {
$properties[$newColumn] = $properties[$column];
unset($properties[$column]);
}
}
$view->set('propertySchema', $properties);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment