Skip to content

Instantly share code, notes, and snippets.

@shahednur
Last active March 30, 2019 14:39
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 shahednur/eabc0f84286e6845dc0d9cefb3bc0a6e to your computer and use it in GitHub Desktop.
Save shahednur/eabc0f84286e6845dc0d9cefb3bc0a6e to your computer and use it in GitHub Desktop.
Enum
in database:
$table->enum('fruit', ['apple', 'banana', 'cantalope']);
in controller's construct:
public function __construct()
{
DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
}
how to use this Enum in cotroller:
function get_enum_values( $table, $field )
{
$type = $this->db->query( "SHOW COLUMNS FROM {$table} WHERE Field = '{$field}'" )->row( 0 )->Type;
preg_match("/^enum\(\'(.*)\'\)$/", $type, $matches);
$enum = explode("','", $matches[1]);
return $enum;
}
public static function getPossibleRoles()
{
$type = DB::select( DB::raw("SHOW COLUMNS FROM v2users WHERE Field = 'role'") )[0]->Type;
preg_match('/^enum\((.*)\)$/', $type, $matches);
$enum = array();
foreach( explode(',', $matches[1]) as $value )
{
$v = trim( $value, "'" );
$enum = array_add($enum, $v, $v);
}
return $enum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment