Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regenerate doesn't work if there is an Embedded property in Entity #160

Closed
welcoMattic opened this issue Apr 20, 2018 · 16 comments
Closed

Comments

@welcoMattic
Copy link
Member

Hi, thanks for maker-bundle, it's a very powerful tool! ❤️

I tried to regenerate an Entity that contains a @ORM\Embedded() property.

Here the steps to reproduce the bug:

  • Run make:entity with some fields
  • Add a @ORM\Embedded() property to this entity
  • Try to regenerate the entity getters/setters

💥

The output is:

$ bin/console make:entity --regenerate --overwrite

 This command will generate any missing methods (e.g. getters & setters) for a class or all classes in a namespace.

 To overwrite any existing methods, re-run this command with the --overwrite flag

 Enter a class or namespace to regenerate [App\Entity]:
 > App\Entity\Product


In ClassMetadataInfo.php line 3325:

  Notice: Trying to get property 'name' of non-object
@welcoMattic
Copy link
Member Author

In fact, make:form fails too if I try to generate a form.

bin/console make:form

 The name of the form class (e.g. TinyPuppyType):
 > FooType


In ClassMetadataInfo.php line 3325:

  Notice: Trying to get property 'name' of non-object

@sadikoff
Copy link
Contributor

@welcoMattic Could you please provide your Entity mapping and check your maker-bundle version, I cannot reproduce this bug

@welcoMattic
Copy link
Member Author

Product.php (Generated entity)

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity()
 * @Vich\Uploadable
 */
class Product
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $reference;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $designation;

    /**
     * @ORM\Embedded(class="CatalogMedia")
     */
    private $mainMedia;

    ...

}

CatalogMedia (Embeddable)

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Embeddable()
 * @Vich\Uploadable
 */
class CatalogMedia
{
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $filename;

    /**
     * @Vich\UploadableField(mapping="catalog_media", fileNameProperty="filename")
     */
    private $file;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $altAttribute;

    ...
}

If execute make:entity --regenerate --overwrite now, there will throw an error.

@weaverryan
Copy link
Member

Hey @welcoMattic!

Thanks for the ♥️ - that's our goal!

Can you verify exactly what version of MakerBundle you're using? We thought we squashed this bug in #149, which was included in v1.4.1.

Cheers!

@welcoMattic
Copy link
Member Author

welcoMattic commented Apr 23, 2018

Hi @weaverryan!

I just updated the bundle to 1.4.2, and I still get the error message.

Here's the complete output of $ bin/console make:entity --regenerate --overwrite -vvv:

This command will generate any missing methods (e.g. getters & setters) for a class or all classes in a namespace.

To overwrite any existing methods, re-run this command with the --overwrite flag

Enter a class or namespace to regenerate [App\Entity]:
> App\Entity\Product


In ClassMetadataInfo.php line 3325:

[ErrorException]
Notice: Trying to get property 'name' of non-object


Exception trace:
Doctrine\ORM\Mapping\ClassMetadataInfo->inlineEmbeddable() at /my_project/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:194
Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at /my_project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:333
Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at /my_project/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:78
Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at /my_project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:226
Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at /my_project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:115
Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata() at /my_project/vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php:124
Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper->getMetadata() at /my_project/vendor/symfony/maker-bundle/src/Doctrine/EntityRegenerator.php:43
Symfony\Bundle\MakerBundle\Doctrine\EntityRegenerator->regenerateEntities() at /my_project/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:778
Symfony\Bundle\MakerBundle\Maker\MakeEntity->regenerateEntities() at /my_project/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:121
Symfony\Bundle\MakerBundle\Maker\MakeEntity->generate() at /my_project/vendor/symfony/maker-bundle/src/Command/MakerCommand.php:93
Symfony\Bundle\MakerBundle\Command\MakerCommand->execute() at /my_project/vendor/symfony/console/Command/Command.php:252
Symfony\Component\Console\Command\Command->run() at /my_project/vendor/symfony/console/Application.php:883
Symfony\Component\Console\Application->doRunCommand() at /my_project/vendor/symfony/framework-bundle/Console/Application.php:86
Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /my_project/vendor/symfony/console/Application.php:241
Symfony\Component\Console\Application->doRun() at /my_project/vendor/symfony/framework-bundle/Console/Application.php:74
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /my_project/vendor/symfony/console/Application.php:143
Symfony\Component\Console\Application->run() at /my_project/bin/console:39

make:entity [--regenerate] [--overwrite] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command> [<name>]

The error comes from doctrine/orm ClassMetadataFactory class. I'm looking for a schema error on my own code 🤔

EDIT: After some of dump, I found that my embedded class CatalogMedia doesn't have reflClass property set. However, doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php need this property in inlineEmbeddable (line 3308 of ClassMetadataInfo.php).

@sadikoff
Copy link
Contributor

I'll check this problem with metedata.

However there is another bug with embedded classes, it refers to embedded field names.

@welcoMattic
Copy link
Member Author

I finally found the cause of this error, and it's not caused by the previous entities. But, by these:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Embeddable()
 * @Vich\Uploadable
 */
class CatalogMedia
{
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $filename;

    /**
     * @Vich\UploadableField(mapping="catalog_media", fileNameProperty="filename")
     */
    private $file;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $altAttribute;

    // classic getters and setters
}

and

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Symfony\Component\HttpFoundation\File\File;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CarouselProductMediaRepository")
 */
class CarouselProductMedia
{
    use ORMBehaviors\Timestampable\Timestampable;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Embedded(class="CatalogMedia")
     */
    private $productMedia;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getProductMedia(): ?CatalogMedia
    {
        return $this->productMedia;
    }

    public function setProductMedia(?CatalogMedia $productMedia): self
    {
        $this->productMedia = $productMedia;

        if ($this->productMedia->getFile() instanceof File) {
            $this->setUpdatedAt(new \DateTime());
        }

        return $this;
    }
}

May be these entities could help us to fix the issue ;)

@dimabory
Copy link

dimabory commented Apr 25, 2018

@welcoMattic Hi

Sorry, but I don't see how you have resolved an issue. I configured classes in the same way like you, but still getting error Notice: Trying to get property of non-object.

Can you explain in details?

Thanks!

@welcoMattic
Copy link
Member Author

Hi @dimabory, I haven't fix the issue yet. I'm still working on it on my free time ;)

@magnetik
Copy link

Facing the same issue, when using make:entity even without regenerate as long as one entity of the bundle has a embedded property.

@sadikoff
Copy link
Contributor

@welcoMattic hi, can you please test my bugfix?

@welcoMattic
Copy link
Member Author

@sadikoff I'll try do test your bug fix this week (next Tuesday is off in France ;) )

@weaverryan
Copy link
Member

@welcoMattic Would you expect the class to be generated with a setter method for your embeddable field? Or, only a getter? (because, the embeddable is initialized in the constructor, so technically, a setter is not needed)

@welcoMattic
Copy link
Member Author

@weaverryan I expect neither getter or setter, just regeneration without errors. Then if we can pass an option to generate a getter for embeddable, it's perfect ;)

weaverryan added a commit that referenced this issue May 2, 2018
…sadikoff)

This PR was squashed before being merged into the 1.0-dev branch (closes #164).

Discussion
----------

Doctrine embedded entity 'make:entity --regenerate' bugfix

Closes #160

Commits
-------

0e36480 Doctrine embedded entity 'make:entity --regenerate' bugfix
@danaki
Copy link

danaki commented Sep 23, 2020

I'm getting the same error Notice: Trying to get property 'name' of non-object.

Exception trace:
  at /Users/up/work/podex/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:3334
 Doctrine\ORM\Mapping\ClassMetadataInfo->inlineEmbeddable() at /Users/up/work/podex/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:197
 Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at /Users/up/work/podex/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:306
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at /Users/up/work/podex/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:81
 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at /Users/up/work/podex/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:193
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at /Users/up/work/podex/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:185
 Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at /Users/up/work/podex/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:306
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at /Users/up/work/podex/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:81
 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at /Users/up/work/podex/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:193
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at /Users/up/work/podex/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:91
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata() at /Users/up/work/podex/vendor/symfony/maker-bundle/src/Doctrine/DoctrineHelper.php:173
 Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper->getMetadata() at /Users/up/work/podex/vendor/symfony/maker-bundle/src/Doctrine/EntityRegenerator.php:48
 Symfony\Bundle\MakerBundle\Doctrine\EntityRegenerator->regenerateEntities() at /Users/up/work/podex/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:779
 Symfony\Bundle\MakerBundle\Maker\MakeEntity->regenerateEntities() at /Users/up/work/podex/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:135
 Symfony\Bundle\MakerBundle\Maker\MakeEntity->generate() at /Users/up/work/podex/vendor/symfony/maker-bundle/src/Command/MakerCommand.php:104
 Symfony\Bundle\MakerBundle\Command\MakerCommand->execute() at /Users/up/work/podex/vendor/symfony/console/Command/Command.php:258
 Symfony\Component\Console\Command\Command->run() at /Users/up/work/podex/vendor/symfony/console/Application.php:934
 Symfony\Component\Console\Application->doRunCommand() at /Users/up/work/podex/vendor/symfony/framework-bundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /Users/up/work/podex/vendor/symfony/console/Application.php:264
 Symfony\Component\Console\Application->doRun() at /Users/up/work/podex/vendor/symfony/framework-bundle/Console/Application.php:82
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /Users/up/work/podex/vendor/symfony/console/Application.php:140
 Symfony\Component\Console\Application->run() at /Users/up/work/podex/bin/console:43

My entity:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
                  xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">

  <entity name="App\Entity\SalaryEntry" table="salary_entries">
    <id name="id" type="uuid" column="id">
      <generator strategy="CUSTOM"/>
      <custom-id-generator class="Ramsey\Uuid\Doctrine\UuidGenerator"/>
    </id>

    <field name="report_date" type="date" column="date" nullable="false"/>
    <field name="payment_wallet" type="string" column="payment_wallet" length="128" nullable="true"/>

    <embedded name="sum" class="Money\Money"/>
    <embedded name="bank" class="Money\Money"/>
    <embedded name="other_wallets" class="Money\Money"/>

    <many-to-one field="contract" target-entity="App\Entity\Contract" fetch="LAZY">
      <join-columns>
        <join-column name="contract_id" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>
    <many-to-one field="payment_method" target-entity="App\Entity\PaymentMethod" fetch="LAZY">
      <join-columns>
        <join-column name="payment_method_id" referenced-column-name="id" nullable="1"/>
      </join-columns>
    </many-to-one>
    <many-to-one field="department" target-entity="App\Entity\Department" fetch="LAZY">
      <join-columns>
        <join-column name="department_id" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>
  </entity>
</doctrine-mapping>

Maker bundle version: v1.21.1

@tiagoppay
Copy link

tiagoppay commented Oct 21, 2020

Hello!!
Same error here!
I'm trying to run bin/console doctrine:mapping:convert xml . -vvv

/app # bin/console doctrine:mapping:convert xml . -vvv
[debug] Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".

[error] Error thrown while running command "doctrine:mapping:convert xml '.' -vvv". Message: "Notice: Trying to get property 'name' of non-object"

[debug] Notified event "console.error" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber::onConsoleError".

[debug] Notified event "console.error" to listener "Symfony\Bundle\MakerBundle\Event\ConsoleErrorSubscriber::onConsoleError".

[debug] Notified event "console.error" to listener "Symfony\Component\Console\EventListener\ErrorListener::onConsoleError".

[debug] Command "doctrine:mapping:convert xml '.' -vvv" exited with code "1"

[debug] Notified event "console.terminate" to listener "Symfony\Bundle\MakerBundle\Event\ConsoleErrorSubscriber::onConsoleTerminate".

[debug] Notified event "console.terminate" to listener "Symfony\Component\Console\EventListener\ErrorListener::onConsoleTerminate".


In ClassMetadataInfo.php line 3334:

  [ErrorException]
  Notice: Trying to get property 'name' of non-object


Exception trace:
  at /app/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:3334
 Doctrine\ORM\Mapping\ClassMetadataInfo->inlineEmbeddable() at /app/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:197
 Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at /app/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:304
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at /app/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:81
 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at /app/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:191
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at /app/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:185
 Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at /app/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:304
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at /app/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:81
 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at /app/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:191
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at /app/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:89
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata() at /app/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php:113
 Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand->execute() at /app/vendor/doctrine/doctrine-bundle/Command/Proxy/ConvertMappingDoctrineCommand.php:37
 Doctrine\Bundle\DoctrineBundle\Command\Proxy\ConvertMappingDoctrineCommand->execute() at /app/vendor/symfony/console/Command/Command.php:258
 Symfony\Component\Console\Command\Command->run() at /app/vendor/symfony/console/Application.php:938
 Symfony\Component\Console\Application->doRunCommand() at /app/vendor/symfony/framework-bundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /app/vendor/symfony/console/Application.php:266
 Symfony\Component\Console\Application->doRun() at /app/vendor/symfony/framework-bundle/Console/Application.php:82
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /app/vendor/symfony/console/Application.php:142
 Symfony\Component\Console\Application->run() at /app/bin/console:43

symfony/maker-bundle: 1.21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants