Last active
March 30, 2019 14:39
-
-
Save shahednur/eabc0f84286e6845dc0d9cefb3bc0a6e to your computer and use it in GitHub Desktop.
Enum
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
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