From 417e2f351b4780f49bb35dcb1e0625202aba7419 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 11 Mar 2021 22:22:42 +0000 Subject: [PATCH] [SchemaDef] Finish association mapping implementation --- .gitignore | 2 + .../Compiler/SchemaDefDriver.php | 58 +++++++++++++------ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 55c62b5398..3f1a83bac5 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,5 @@ social.local.yaml # V2 config.php /file + +notes \ No newline at end of file diff --git a/src/DependencyInjection/Compiler/SchemaDefDriver.php b/src/DependencyInjection/Compiler/SchemaDefDriver.php index d84ab50f21..3d217560c1 100644 --- a/src/DependencyInjection/Compiler/SchemaDefDriver.php +++ b/src/DependencyInjection/Compiler/SchemaDefDriver.php @@ -101,39 +101,59 @@ class SchemaDefDriver extends StaticPHPDriver implements CompilerPassInterface ]); foreach ($schema['fields'] as $name => $opts) { - if ($opts['type'] === 'foreign key') { + $unique = null; + foreach ($schema['unique keys'] ?? [] as $key => $uniq_arr) { + if (in_array($name, $uniq_arr)) { + $unique = $key; + break; + } + } + + 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([ - 'fieldName' => $name, - 'targetEntity' => $target_entity, - 'joinColumns' => [[ - 'name' => $name, - 'referencedColumnName' => $target_field, - ]], - ]); + $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: - dd('Not yet implemented'); + 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']]; - $unique = null; - foreach ($schema['unique keys'] ?? [] as $key => $uniq_arr) { - if (in_array($name, $uniq_arr)) { - $unique = $key; - break; - } - } - + $type = self::types[($opts['size'] ?? '') . $opts['type']]; $default = $opts['default'] ?? null; $field = [