Created
November 15, 2021 09:01
-
-
Save miyacorata/cdcf4878b9356ed60f36c1fd1f3ede5c to your computer and use it in GitHub Desktop.
データベースのスキーマからバリデーションルールを出すやつ
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 | |
// CC0 とします。好き勝手にするといいよ! | |
const USER = "UserName"; | |
const PASS = "YourP@ssW0rd!"; | |
const HOST = "localhost"; | |
const TABLE = "some_table"; | |
try{ | |
$pdo = new PDO('mysql:dbname=information_schema;host='.HOST.';port=3306', USER, PASS); | |
$res = $pdo->query("select COLUMN_NAME,CHARACTER_MAXIMUM_LENGTH from information_schema.COLUMNS where TABLE_NAME = '".TABLE."' and DATA_TYPE = 'varchar'"); | |
$res->execute(); | |
foreach($res->fetchAll() as $row){ | |
echo <<<DOC | |
'{$row['COLUMN_NAME']}' => [ | |
'max:{$row['CHARACTER_MAXIMUM_LENGTH']}', | |
], | |
DOC; | |
} | |
}catch(PDOException $e){ | |
exit($e->getMessage()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment