doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.94k stars 2.52k forks source link

Warning: Invalid argument supplied for foreach() #8344

Open paulhagen opened 3 years ago

paulhagen commented 3 years ago

I ran into this issue while trying to create a migration from an entity. The attribute causing the error (a Many-To-One, Unidirectional) on doctrine:schema:validate

/**
     * @ORM\ManyToOne(targetEntity="Kontakt")
     * @JoinColumn(name="kontakt_id", referencedColumnName="id")
     */
    private Kontakt $kontakt;

The error

The exception
Mapping
-------

In CachedReader.php line 107:

  [ErrorException]                                  
  Warning: Invalid argument supplied for foreach()  

Exception trace:
  at /var/www/html/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/CachedReader.php:107
 Doctrine\Common\Annotations\CachedReader->getPropertyAnnotation() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php:293
 Doctrine\ORM\Mapping\Driver\AnnotationDriver->loadMetadataForClass() at /var/www/html/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/MappingDriverChain.php:79
 Doctrine\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:155
 Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at /var/www/html/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:306
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:82
 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at /var/www/html/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:184
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at /var/www/html/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:90
 Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/SchemaValidator.php:68
 Doctrine\ORM\Tools\SchemaValidator->validateMapping() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php:69
 Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand->execute() at /var/www/html/vendor/doctrine/doctrine-bundle/Command/Proxy/ValidateSchemaCommand.php:34
 Doctrine\Bundle\DoctrineBundle\Command\Proxy\ValidateSchemaCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:258
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:938
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:266
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:82
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:142
 Symfony\Component\Console\Application->run() at /var/www/html/bin/console:43

The mapping for itself is correctly. If I remove the attribute with the annotation and run do:sc:va doctrine say [OK] The mapping files are correct.

If I undo the deletion and then invoke the command again the same message comes up. If I repeat this procedure again, the above error occurs again.

I can now reproduce the error as often as I want in this way.

stof commented 3 years ago

which attribute are you talking about ?

paulhagen commented 3 years ago
Full entity
/**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer", name="id")
     */
    private int $id;

    /**
     * @ORM\ManyToOne(targetEntity="Druckerei")
     * @ORM\JoinColumn(name="druckerei_id", referencedColumnName="id")
     */
    private Druckerei $druckerei;

    /**
     * @ORM\ManyToMany(targetEntity="Sujet")
     * @ORM\JoinTable(name="druckauftraege_sujets",
     *     joinColumns={@ORM\JoinColumn(name="druckauftrag_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="sujet_id", referencedColumnName="id", unique=true)}
     *     )
     */
    private Collection $sujets;

    /**
     * @ORM\ManyToOne(targetEntity="Kontakt")
     * @JoinColumn(name="kontakt_id", referencedColumnName="id")
     */
    private Kontakt $kontakt;

    /**
     * @Column(type="string", nullable=true)
     */
    private ?string $bemerkung;

The attribute $kontakt is causing the issue.

ostrolucky commented 3 years ago

I'm sorry about there isn't anything in your post which would indicate this would be a problem in doctrine-bundle. Moving to ORM.

lcobucci commented 3 years ago

@paulhagen it looks like you're using the cached annotation reader. Which cache driver you have configured? Have you tried to clean it up?

Also, you sometimes use the ORM namespace on the annotation and sometimes don't. I suggest you to make it consistent:

     /**
      * @ORM\ManyToOne(targetEntity="Kontakt")
-     * @JoinColumn(name="kontakt_id", referencedColumnName="id")
+     * @ORM\JoinColumn(name="kontakt_id", referencedColumnName="id")
      */
     private Kontakt $kontakt;