Webonaute / DoctrineFixturesGeneratorBundle

Generate Fixture from your existing data in your database. You can specify the Entity name and the IDs you want to import in your fixture.
63 stars 47 forks source link

Error processing PersistentCollection as a sub-item #13

Closed bjrothman closed 9 years ago

bjrothman commented 9 years ago

Recently ran into an issue when trying to create a fixture from some existing entities. I fixed the issue myself and though you may want to merge the fix into the master branch.

The error: PHP Fatal error: Call to undefined method Doctrine\ORM\PersistentCollection::getId() in /vendor/webonaute/doctrine-fixtures-generator-bundle/Webonaute/DoctrineFixturesGeneratorBundle/Tool/FixtureGenerator.php on line 262

The Fix: (replace the appropriate lines of FixtureGenerator.php)

    public function generateFixtureItemStub($item)
    {
        $id = $item->getId();

        $code = "";
        $reflexion = new \ReflectionClass($item);
        $properties = $reflexion->getProperties();

        foreach ($properties as $property) {
            $property->setAccessible(true);
            $name = $property->getName();
            if (strpos($name, '_')) {
                $_names = explode('_', $property->getName());
                foreach ($_names as $k => $_name) {
                    $_names[$k] = ucfirst($_name);
                }
                $name = implode('', $_names);
            }
            $setter = "set" . ucfirst($name);
            $getter = "get" . ucfirst($name);
            $comment = "";
            if (method_exists($item, $setter)) {
                $value = $property->getValue($item);

                if (is_integer($value)) {
                    $setValue = $value;
                } elseif (is_bool($value)) {
                    if ($value === true) {
                        $setValue = 1;
                    } else {
                        $setValue = 0;
                    }
                } elseif ($value instanceof \DateTime) {
                    $setValue = "new \\DateTime(\"" . $value->format("Y-m-d H:i:s") . "\")";
                } elseif (is_object($value) && get_class($value) != "Doctrine\\ORM\\PersistentCollection") {
                    //check reference.
                    $relatedReflexion = new \ReflectionClass($value);
                    $relatedId = $value->getId();
                    $setValue = "\$this->getReference('{$this->referencePrefix}{$relatedReflexion->getShortName()}_{$relatedId}')";
                    $comment = "";
                }
                elseif(is_object($value) && get_class($value) == "Doctrine\\ORM\\PersistentCollection"){
                    $setValue = "unserialize('" . serialize($value->getSnapshot()) . "')";
                }
                elseif (is_array($value)) {
                    $setValue = "unserialize('" . serialize($value) . "')";
                } elseif (is_null($value)) {
                    $setValue = "NULL";
                } else {
                    $setValue = '"' . $value . '"';
                }

                $code .= "\n<spaces><spaces>{$comment}\$item{$id}->{$setter}({$setValue});";
            }
        }

        $code .= "\n\n<spaces><spaces>\$this->addReference('{$this->referencePrefix}{$reflexion->getShortName()}_{$id}', \$item{$id});\n";

        return $code;
    }
Webonaute commented 9 years ago

added the fix thank.