[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,39 +101,59 @@ class SchemaDefDriver extends StaticPHPDriver implements CompilerPassInterface
]); ]);
foreach ($schema['fields'] as $name => $opts) { 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 // See Doctrine\ORM\Mapping::associationMappings
// TODO still need to map nullability, comment, fk name and such, but // TODO still need to map nullability, comment, fk name and such, but
// the interface doesn't seem to support it currently // the interface doesn't seem to support it currently
list($target_entity, $target_field) = explode('.', $opts['target']); 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']) { switch ($opts['multiplicity']) {
case 'one to one': case 'one to one':
$metadata->mapOneToOne([ $metadata->mapOneToOne($map);
'fieldName' => $name, break;
'targetEntity' => $target_entity, case 'many to one':
'joinColumns' => [[ $metadata->mapManyToOne($map);
'name' => $name, break;
'referencedColumnName' => $target_field, case 'one to many':
]], $map['mappedBy'] = $target_field;
]); $metadata->mapOneToMany($map);
break;
case 'many to many':
$metadata->mapManyToMany($map);
break; break;
default: default:
dd('Not yet implemented'); throw new \Exception('Invalid multiplicity specified: ' . $opts['multiplicity']);
} }
} else { } else {
// Convert old to new types // Convert old to new types
// For ints, prepend the size (smallint) // For ints, prepend the size (smallint)
// The size field doesn't exist otherwise // The size field doesn't exist otherwise
$type = self::types[($opts['size'] ?? '') . $opts['type']]; $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; $default = $opts['default'] ?? null;
$field = [ $field = [