Jelajahi Sumber

post MR fixes

Olivier Massot 11 bulan lalu
induk
melakukan
3bf54c517e
1 mengubah file dengan 54 tambahan dan 16 penghapusan
  1. 54 16
      src/Service/Doctrine/SchemaValidation/SchemaSnippetsMaker.php

+ 54 - 16
src/Service/Doctrine/SchemaValidation/SchemaSnippetsMaker.php

@@ -387,6 +387,7 @@ class SchemaSnippetsMaker
         $prop = new Property($name);
         $prop->setProtected();
         $prop->setType($php_type);
+        $prop->setComment("-- Warning : auto-generated property, checkup the attribute options --");
 
         if ($type === 'text') {
             $prop->addAttribute(Column::class, ['length' => 255, 'options' => ['nullable' => true]]);
@@ -466,8 +467,6 @@ class SchemaSnippetsMaker
 
         if (isset($type['cascade'])) {
             $options['cascade'] = $type['cascade'];
-        } else {
-            $options['cascade'] = ['persist'];
         }
 
         if (isset($type['inversedBy'])) {
@@ -500,7 +499,7 @@ class SchemaSnippetsMaker
         }
 
         $options = [];
-        if ($type['joinTable']['name']) {
+        if (isset($type['joinTable']['name']) && $type['joinTable']['name']) {
             $options['name'] = $type['joinTable']['name'];
         }
 
@@ -649,6 +648,41 @@ class SchemaSnippetsMaker
         return null;
     }
 
+    protected function getInverseSetterCallFromCollectionProp(Property $prop, bool $isRemoving = false): ?string
+    {
+        if (
+            $prop->getType() !== Collection::class
+        ) {
+            throw new \LogicException('The property must be a collection');
+        }
+
+        $relationAttr = null;
+        foreach ($prop->getAttributes() as $attribute) {
+            if ($attribute->getName() === OneToMany::class || $attribute->getName() === ManyToMany::class) {
+                $relationAttr = $attribute;
+            }
+        }
+
+        if (!$relationAttr) {
+            throw new \LogicException('Missing relation attribute for collection property ' . $prop->getName());
+        }
+
+        $inversedBy = $relationAttr->getArguments()['inversedBy'] ?? $relationAttr->getArguments()['mappedBy'] ?? null;
+        if (!$inversedBy) {
+            var_dump('Could not determine the inverse prop for collection property ' . $prop->getName());
+            $inversedBy = 'XXXX';
+        }
+
+        $attr = $prop->getAttributes()[0];
+
+        $prefix = ($attr->getName() === OneToMany::class ? 'set' : ($isRemoving ? 'remove' : 'add'));
+
+        return
+            $prefix .
+            ucfirst($inversedBy) .
+            (($prefix === 'set' && $isRemoving) ? '(null)' : '($this)');
+    }
+
     /**
      * Make an 'adder' method for the given property
      *
@@ -667,19 +701,19 @@ class SchemaSnippetsMaker
         $parameter->setType($targetEntityName ?? 'mixed');
         $method->setParameters([$parameter]);
 
+        $inverseSetterCall = $this->getInverseSetterCallFromCollectionProp($prop);
+
         $method->setReturnType('self');
-        $method->setBody(
-            implode(
-                "\n",
-                [
-                    'if (!$this->' . $prop->getName() . '->contains($' . $singularPropName . ')) {',
-                    '    $this->' . $prop->getName() . '[] = $' . $singularPropName . ';',
-                    '}',
-                    '',
-                    'return $this;',
-                ]
-            )
-        );
+        $method->setBody(implode(
+            "\n",
+            [
+                'if (!$this->' . $prop->getName() . '->contains($' . $singularPropName . ')) {',
+                '    $this->' . $prop->getName() . '[] = $' . $singularPropName . ';',
+                '    $' . $singularPropName . '->' . $inverseSetterCall . ';',
+                '}',
+                '',
+                'return $this;',
+            ]));
         return $method;
     }
 
@@ -700,12 +734,16 @@ class SchemaSnippetsMaker
         $parameter->setType($targetEntityName ?? 'mixed');
         $method->setParameters([$parameter]);
 
+        $inverseSetterCall = $this->getInverseSetterCallFromCollectionProp($prop, true);
+
         $method->setReturnType('self');
         $method->setBody(
             implode(
                 "\n",
                 [
-                    '$this->' . $prop->getName() . '->removeElement($' . $singularPropName . ');',
+                    'if ($this->' . $prop->getName() . '->removeElement($' . $singularPropName . ')) {',
+                    '    $' . $singularPropName . '->' . $inverseSetterCall . ';',
+                    '}',
                     '',
                     'return $this;',
                 ]