Last active
October 30, 2019 03:15
-
-
Save nojimage/a71e9c1d47e0e76d6dc79fbec721d33a to your computer and use it in GitHub Desktop.
Bake Entity with column comment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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