Warning: Invalid argument supplied for foreach() #6569

Open
opened 2026-01-22 15:35:04 +01:00 by admin · 4 comments
Owner

Originally created by @paulhagen on GitHub (Nov 20, 2020).

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.

Originally created by @paulhagen on GitHub (Nov 20, 2020). 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 ```php /** * @ORM\ManyToOne(targetEntity="Kontakt") * @JoinColumn(name="kontakt_id", referencedColumnName="id") */ private Kontakt $kontakt; ``` The error <details><summary>The exception</summary> <pre> 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 </pre> </details> 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.
Author
Owner

@stof commented on GitHub (Nov 20, 2020):

which attribute are you talking about ?

@stof commented on GitHub (Nov 20, 2020): which attribute are you talking about ?
Author
Owner

@paulhagen commented on GitHub (Nov 20, 2020):

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.

@paulhagen commented on GitHub (Nov 20, 2020): <details><summary>Full entity</summary><pre> /** * @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; </pre></details> The attribute ```$kontakt``` is causing the issue.
Author
Owner

@ostrolucky commented on GitHub (Nov 20, 2020):

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.

@ostrolucky commented on GitHub (Nov 20, 2020): 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.
Author
Owner

@lcobucci commented on GitHub (Nov 20, 2020):

@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;
@lcobucci commented on GitHub (Nov 20, 2020): @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: ```diff /** * @ORM\ManyToOne(targetEntity="Kontakt") - * @JoinColumn(name="kontakt_id", referencedColumnName="id") + * @ORM\JoinColumn(name="kontakt_id", referencedColumnName="id") */ private Kontakt $kontakt; ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6569