Skip to content

Instantly share code, notes, and snippets.

@miyacorata
Created November 15, 2021 09:01
Show Gist options
  • Save miyacorata/cdcf4878b9356ed60f36c1fd1f3ede5c to your computer and use it in GitHub Desktop.
Save miyacorata/cdcf4878b9356ed60f36c1fd1f3ede5c to your computer and use it in GitHub Desktop.
データベースのスキーマからバリデーションルールを出すやつ
<?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