Skip to content

Instantly share code, notes, and snippets.

@bklein01
Last active March 9, 2024 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bklein01/0b80320ab19491fe4827c4e555c5c7c7 to your computer and use it in GitHub Desktop.
Save bklein01/0b80320ab19491fe4827c4e555c5c7c7 to your computer and use it in GitHub Desktop.
The following quick script will generate a model given a table name or all models given a database name, with all getters and setters. This is a big help for those us who develop using the new version of Symfony that removed the reverse engineering feature. Just modify two places. Working on a way to do all tables in a schema in one shot..
SELECT
CONCAT_WS(
'\n',
'namespace App\\Entity;',
'\n',
CONCAT(
'class ',
CONCAT(UCASE(SUBSTRING(s.table_name, 1, 1)), LOWER(SUBSTRING(s.table_name, 2))),
' {'
),
GROUP_CONCAT(q.details SEPARATOR '\n'),
'}',
'\n'
) details
FROM information_schema.tables s
JOIN (
SELECT
t.table_name, t.table_schema,
CONCAT_WS(
'\n',
CONCAT(
'protected $',
col.column_name,
';'
),
'\n',
CONCAT(
'public function get_',
col.column_name,
'() {'
),
CONCAT(
' return $this->',
col.column_name,
';'
),
'}',
'\n',
CONCAT(
'public function set_',
col.column_name,
'($value) {'
),
CONCAT(
' $this->',
col.column_name,
' = $value;'
),
'}',
'\n'
) details
FROM
information_schema.columns col
INNER JOIN information_schema.tables t
ON col.table_schema = t.table_schema
AND col.table_name = t.table_name) q ON s.table_name = q.table_name AND s.table_schema = q.table_schema
WHERE s.table_schema = '<DATABASENAME>'
GROUP BY s.table_name
SELECT
CONCAT_WS(
'\n',
'namespace App\\Entity;',
'\n',
CONCAT(
'class ',
IFNULL('<CLASSNAME>', ''),
' {'
)
)
UNION
ALL
SELECT
CONCAT_WS(
'\n',
CONCAT(
'protected $',
IFNULL(col.column_name, ''),
';'
),
'\n',
CONCAT(
'public function get_',
IFNULL(col.column_name, ''),
'() {'
),
CONCAT(
' return $this->',
IFNULL(col.column_name, ''),
';'
),
'}',
'\n',
CONCAT(
'public function set_',
IFNULL(col.column_name, ''),
'($value) {'
),
CONCAT(
' $this->',
IFNULL(col.column_name, ''),
' = $value;'
),
'}',
'\n'
)
FROM
information_schema.columns col
INNER JOIN information_schema.tables t
ON col.table_schema = t.table_schema
AND col.table_name = t.table_name
WHERE col.table_name = '<TABLE NAME>' AND col.table_schema = '<DATABASENAME>'
UNION
ALL
SELECT
'}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment