[DOCTRINE][SCHEMADEF] Small refactoring
This commit is contained in:
parent
2c9a732256
commit
1b45936f19
@ -4,6 +4,7 @@ namespace App\Util;
|
|||||||
|
|
||||||
use Doctrine\Persistence\Mapping\ClassMetadata;
|
use Doctrine\Persistence\Mapping\ClassMetadata;
|
||||||
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
|
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
|
||||||
|
use Functional as F;
|
||||||
|
|
||||||
class SchemaDefDriver extends StaticPHPDriver
|
class SchemaDefDriver extends StaticPHPDriver
|
||||||
{
|
{
|
||||||
@ -49,27 +50,45 @@ class SchemaDefDriver extends StaticPHPDriver
|
|||||||
|
|
||||||
foreach ($schema['fields'] as $name => $opts) {
|
foreach ($schema['fields'] as $name => $opts) {
|
||||||
// Convert old to new types
|
// Convert old to new types
|
||||||
$type = $name === 'date'
|
$type = // $name === 'date'
|
||||||
// Old date fields were stored as int, store as datetime/timestamp
|
// // Old date fields were stored as int, store as datetime/timestamp
|
||||||
? 'datetime'
|
// ? 'datetime'
|
||||||
// For ints, prepend the size (smallint)
|
// // For ints, prepend the size (smallint)
|
||||||
// The size fields doesn't exist otherwise, suppress error
|
// // The size field doesn't exist otherwise
|
||||||
: self::types[(@$opts['size']) . $opts['type']];
|
// :
|
||||||
|
self::types[($opts['size'] ?? '') . $opts['type']];
|
||||||
|
|
||||||
|
$unique = null;
|
||||||
|
foreach ($schema['unique keys'] as $key => $uniq_arr) {
|
||||||
|
if (in_array($name, $uniq_arr)) {
|
||||||
|
$unique = $key;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$field = [
|
$field = [
|
||||||
'id' => in_array($name, $schema['primary key']),
|
// boolean, optional
|
||||||
|
'id' => in_array($name, $schema['primary key']),
|
||||||
|
// string
|
||||||
'fieldName' => $name,
|
'fieldName' => $name,
|
||||||
'type' => $type,
|
// string
|
||||||
'unique' => in_array([$name], $schema['unique keys']) || @$opts['unique'],
|
'type' => $type,
|
||||||
// String length, ignored if not a string, suppress error
|
// stringn, optional
|
||||||
'length' => @$opts['length'],
|
'unique' => $unique,
|
||||||
'nullable' => (@!$opts['not null']),
|
// String length, ignored if not a string
|
||||||
// Numeric precision and scale, ignored if not a number, suppress errors
|
// int, optional
|
||||||
'precision' => @$opts['precision'],
|
'length' => $opts['length'] ?? null,
|
||||||
'scale' => @$opts['scale'],
|
// boolean, optional
|
||||||
'options' => [
|
'nullable' => !($opts['not null'] ?? false),
|
||||||
|
// Numeric precision and scale, ignored if not a number
|
||||||
|
// integer, optional
|
||||||
|
'precision' => $opts['precision'] ?? null,
|
||||||
|
// integer, optional
|
||||||
|
'scale' => $opts['scale'] ?? null,
|
||||||
|
'options' => [
|
||||||
'comment' => $opts['description'],
|
'comment' => $opts['description'],
|
||||||
'default' => @$opts['default'],
|
'default' => $opts['default'] ?? null,
|
||||||
'unsigned' => @$opts['unsigned'],
|
'unsigned' => $opts['unsigned'] ?? null,
|
||||||
// 'fixed' => bool, unused
|
// 'fixed' => bool, unused
|
||||||
// 'collation' => string, unused
|
// 'collation' => string, unused
|
||||||
// 'check', unused
|
// 'check', unused
|
||||||
@ -77,12 +96,11 @@ class SchemaDefDriver extends StaticPHPDriver
|
|||||||
// 'columnDefinition', unused
|
// 'columnDefinition', unused
|
||||||
];
|
];
|
||||||
// The optional feilds from earlier were populated with null, remove them
|
// The optional feilds from earlier were populated with null, remove them
|
||||||
$field = array_filter($field, function ($v) { return !is_null($v); });
|
$field = array_filter($field, F\not('is_null'));
|
||||||
$field['options'] = array_filter($field['options'], function ($v) { return !is_null($v); });
|
$field['options'] = array_filter($field['options'], F\not('is_null'));
|
||||||
|
|
||||||
$metadata->mapField($field);
|
$metadata->mapField($field);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO foreign keys
|
// TODO foreign keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user