[SchemaDef] Finish association mapping implementation
This commit is contained in:
parent
1d42c7a835
commit
417e2f351b
2
.gitignore
vendored
2
.gitignore
vendored
@ -40,3 +40,5 @@ social.local.yaml
|
|||||||
# V2
|
# V2
|
||||||
config.php
|
config.php
|
||||||
/file
|
/file
|
||||||
|
|
||||||
|
notes
|
@ -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 = [
|
||||||
|
Loading…
Reference in New Issue
Block a user