[SchemaDef] Finish association mapping implementation

This commit is contained in:
Hugo Sales 2021-03-11 22:22:42 +00:00
parent 1d42c7a835
commit 417e2f351b
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 41 additions and 19 deletions

2
.gitignore vendored
View File

@ -40,3 +40,5 @@ social.local.yaml
# V2 # V2
config.php config.php
/file /file
notes

View File

@ -101,31 +101,6 @@ class SchemaDefDriver extends StaticPHPDriver implements CompilerPassInterface
]); ]);
foreach ($schema['fields'] as $name => $opts) { foreach ($schema['fields'] as $name => $opts) {
if ($opts['type'] === 'foreign key') {
// See Doctrine\ORM\Mapping::associationMappings
// TODO still need to map nullability, comment, fk name and such, but
// the interface doesn't seem to support it currently
list($target_entity, $target_field) = explode('.', $opts['target']);
switch ($opts['multiplicity']) {
case 'one to one':
$metadata->mapOneToOne([
'fieldName' => $name,
'targetEntity' => $target_entity,
'joinColumns' => [[
'name' => $name,
'referencedColumnName' => $target_field,
]],
]);
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; $unique = null;
foreach ($schema['unique keys'] ?? [] as $key => $uniq_arr) { foreach ($schema['unique keys'] ?? [] as $key => $uniq_arr) {
if (in_array($name, $uniq_arr)) { if (in_array($name, $uniq_arr)) {
@ -134,6 +109,51 @@ class SchemaDefDriver extends StaticPHPDriver implements CompilerPassInterface
} }
} }
if ($opts['foreign key'] ?? false) {
foreach (['target', 'multiplicity'] as $f) {
if (!isset($opts[$f])) {
throw new \Exception("{$class_name}.{$name} doesn't have the required field `{$f}`");
}
}
// See Doctrine\ORM\Mapping::associationMappings
// TODO still need to map nullability, comment, fk name and such, but
// the interface doesn't seem to support it currently
list($target_entity, $target_field) = explode('.', $opts['target']);
$map = [
'fieldName' => $name,
'targetEntity' => $target_entity,
'joinColumns' => [[
'name' => $name,
'referencedColumnName' => $target_field,
]],
'id' => in_array($name, $schema['primary key']),
'unique' => $unique,
];
switch ($opts['multiplicity']) {
case 'one to one':
$metadata->mapOneToOne($map);
break;
case 'many to one':
$metadata->mapManyToOne($map);
break;
case 'one to many':
$map['mappedBy'] = $target_field;
$metadata->mapOneToMany($map);
break;
case 'many to many':
$metadata->mapManyToMany($map);
break;
default:
throw new \Exception('Invalid multiplicity specified: ' . $opts['multiplicity']);
}
} 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']];
$default = $opts['default'] ?? null; $default = $opts['default'] ?? null;
$field = [ $field = [