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