forked from GNUsocial/gnu-social
[SCHEMADEF] Add preliminary support for foreign keys
This commit is contained in:
parent
f486656756
commit
b337d6b2eb
@ -31,7 +31,6 @@
|
|||||||
|
|
||||||
namespace App\DependencyInjection\Compiler;
|
namespace App\DependencyInjection\Compiler;
|
||||||
|
|
||||||
use App\Core\Log;
|
|
||||||
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;
|
use Functional as F;
|
||||||
@ -94,8 +93,6 @@ class SchemaDefDriver extends StaticPHPDriver implements CompilerPassInterface
|
|||||||
{
|
{
|
||||||
$schema = $class_name::schemaDef();
|
$schema = $class_name::schemaDef();
|
||||||
|
|
||||||
Log::emergency($class_name);
|
|
||||||
|
|
||||||
$metadata->setPrimaryTable([
|
$metadata->setPrimaryTable([
|
||||||
'name' => $schema['name'],
|
'name' => $schema['name'],
|
||||||
'indexes' => self::kv_to_name_col($schema['indexes'] ?? []),
|
'indexes' => self::kv_to_name_col($schema['indexes'] ?? []),
|
||||||
@ -104,66 +101,81 @@ class SchemaDefDriver extends StaticPHPDriver implements CompilerPassInterface
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
foreach ($schema['fields'] as $name => $opts) {
|
foreach ($schema['fields'] as $name => $opts) {
|
||||||
// TODO
|
if ($opts['type'] === 'foreign key') {
|
||||||
// Convert old to new types
|
// See Doctrine\ORM\Mapping::associationMappings
|
||||||
$type = $name === 'date'
|
|
||||||
// Old date fields were stored as int, store as datetime/timestamp
|
|
||||||
? 'datetime'
|
|
||||||
// For ints, prepend the size (smallint)
|
|
||||||
// The size field doesn't exist otherwise
|
|
||||||
:
|
|
||||||
self::types[($opts['size'] ?? '') . $opts['type']];
|
|
||||||
|
|
||||||
$unique = null;
|
// TODO still need to map nullability, comment, fk name and such, but
|
||||||
foreach ($schema['unique keys'] ?? [] as $key => $uniq_arr) {
|
// the interface doesn't seem to support it currently
|
||||||
if (in_array($name, $uniq_arr)) {
|
list($target_entity, $target_field) = explode('.', $opts['target']);
|
||||||
$unique = $key;
|
switch ($opts['multiplicity']) {
|
||||||
|
case 'one to one':
|
||||||
|
$metadata->mapOneToOne([
|
||||||
|
'fieldName' => $name,
|
||||||
|
'targetEntity' => $target_entity,
|
||||||
|
'joinColumns' => [[
|
||||||
|
'name' => $name,
|
||||||
|
'referencedColumnName' => $target_field,
|
||||||
|
]],
|
||||||
|
]);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
dd('Not yet implemented');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Convert old to new types
|
||||||
|
// For ints, prepend the size (smallint)
|
||||||
|
// The size field doesn't exist otherwise
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$default = $opts['default'] ?? null;
|
||||||
|
|
||||||
|
$field = [
|
||||||
|
// boolean, optional
|
||||||
|
'id' => in_array($name, $schema['primary key']),
|
||||||
|
// string
|
||||||
|
'fieldName' => $name,
|
||||||
|
// string
|
||||||
|
'type' => $type,
|
||||||
|
// string, optional
|
||||||
|
'unique' => $unique,
|
||||||
|
// String length, ignored if not a string
|
||||||
|
// int, optional
|
||||||
|
'length' => $opts['length'] ?? null,
|
||||||
|
// boolean, optional
|
||||||
|
'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'] ?? null,
|
||||||
|
'default' => $default,
|
||||||
|
'unsigned' => $opts['unsigned'] ?? null,
|
||||||
|
// bool, optional
|
||||||
|
'fixed' => $opts['type'] === 'char',
|
||||||
|
// 'collation' => string, unused
|
||||||
|
// 'check', unused
|
||||||
|
],
|
||||||
|
// 'columnDefinition', unused
|
||||||
|
];
|
||||||
|
// The optional feilds from earlier were populated with null, remove them
|
||||||
|
$field = array_filter($field, F\not('is_null'));
|
||||||
|
$field['options'] = array_filter($field['options'], F\not('is_null'));
|
||||||
|
|
||||||
|
$metadata->mapField($field);
|
||||||
|
if ($opts['type'] === 'serial') {
|
||||||
|
$metadata->setIdGeneratorType($metadata::GENERATOR_TYPE_AUTO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$default = $opts['default'] ?? null;
|
|
||||||
|
|
||||||
$field = [
|
|
||||||
// boolean, optional
|
|
||||||
'id' => in_array($name, $schema['primary key']),
|
|
||||||
// string
|
|
||||||
'fieldName' => $name,
|
|
||||||
// string
|
|
||||||
'type' => $type,
|
|
||||||
// stringn, optional
|
|
||||||
'unique' => $unique,
|
|
||||||
// String length, ignored if not a string
|
|
||||||
// int, optional
|
|
||||||
'length' => $opts['length'] ?? null,
|
|
||||||
// boolean, optional
|
|
||||||
'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'] ?? null,
|
|
||||||
'default' => $default,
|
|
||||||
'unsigned' => $opts['unsigned'] ?? null,
|
|
||||||
// bool, optional
|
|
||||||
'fixed' => $opts['type'] === 'char',
|
|
||||||
// 'collation' => string, unused
|
|
||||||
// 'check', unused
|
|
||||||
],
|
|
||||||
// 'columnDefinition', unused
|
|
||||||
];
|
|
||||||
// The optional feilds from earlier were populated with null, remove them
|
|
||||||
$field = array_filter($field, F\not('is_null'));
|
|
||||||
$field['options'] = array_filter($field['options'], F\not('is_null'));
|
|
||||||
|
|
||||||
$metadata->mapField($field);
|
|
||||||
if ($opts['type'] === 'serial') {
|
|
||||||
$metadata->setIdGeneratorType($metadata::GENERATOR_TYPE_AUTO);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// TODO foreign keys
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user