Hello,
how is it possible to create an array containing the mysql column name and the respective sql comment of this column?
Cake 4+
Hello,
how is it possible to create an array containing the mysql column name and the respective sql comment of this column?
Cake 4+
On your table class you have a getSchema()
method. That will give you and array to get at the details you want.
Thanks for the tip, I can’t get any further with getSchema()… But I found a solution:
//get column- names and comments in array from table
public function getColumnNamesAndColumnCommentsAsArrayFromTable($myTableName) {
//Costum SQL Statement
$connection = ConnectionManager::get('default');
$results = $connection
->newQuery()
->select('column_name,column_comment')
->from('information_schema.`COLUMNS`')
->where('TABLE_NAME ="'.$myTableName.'"')
->execute()
->fetchAll('assoc');
//build new array
$newArray = [];
foreach ($results AS $key => $value) {
//if no comment exist continue
if ($value['column_comment']=='') {
continue;
}
$newArray[$value['column_name']] = $value['column_comment'];
}
return $newArray;
}
$columnNamesAndColumnComments = getColumnNamesAndColumnCommentsAsArrayFromTable($myTableName);