Unable to use other annotations than ORM #6648

Closed
opened 2026-01-22 15:36:29 +01:00 by admin · 2 comments
Owner

Originally created by @ThibautSF on GitHub (Mar 12, 2021).

Hi,

I'm unable to use any other annotation than ORM ones on my entities. And after several days I didn't manage to find a satisfying solution.

Here is an example with the "Symfony\Component\Serializer\Annotation\Groups" annotation.
The following fatal error is thrown :

[Semantical Error] The annotation "@Symfony\Component\Serializer\Annotation\Groups" in property MyProject\Entities\ATest::$id was never imported. Did you maybe forget to add a "use" statement for this annotation? 

Here is the most basic structure for my entities

<?php

namespace MyProject\Entities;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity
 * @ORM\Table(name="test")
 */
class ATest implements \Serializable
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"identifier"})
     */
    private $id;

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

    /**
     * {@inheritdoc}
     */
    public function serialize(): string
    {
        return serialize([$this->id]);
    }

    /**
     * {@inheritdoc}
     */
    public function unserialize($serialized): void
    {
        [$this->id] = unserialize($serialized, ['allowed_classes' => false]);
    }
}

My entitymanager is generated like that

$paths = [__DIR__ . '/../../src/Entities']; //path to entities

$dbParams = [
    'host' => DB_HOST,
    'driver' => DB_DRIVER,
    'user' => DB_USERNAME,
    'password' => DB_PASSWORD,
    'dbname' => DB_DATABASE,
];

$config = Setup::createAnnotationMetadataConfiguration($paths, DEV_MODE, null, null, false);
$this->entityManager = EntityManager::create($dbParams, $config);

Installed packages versions :

  • doctrine/orm 2.8.2
  • symfony/serializer 5.2.4 (from symfony/serializer-pack)

Thanks for reading.

Originally created by @ThibautSF on GitHub (Mar 12, 2021). Hi, I'm unable to use any other annotation than ORM ones on my entities. And after several days I didn't manage to find a satisfying solution. Here is an example with the "Symfony\Component\Serializer\Annotation\Groups" annotation. The following fatal error is thrown : ``` [Semantical Error] The annotation "@Symfony\Component\Serializer\Annotation\Groups" in property MyProject\Entities\ATest::$id was never imported. Did you maybe forget to add a "use" statement for this annotation? ``` Here is the most basic structure for my entities ```php <?php namespace MyProject\Entities; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; /** * @ORM\Entity * @ORM\Table(name="test") */ class ATest implements \Serializable { /** * @var int * * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") * @Groups({"identifier"}) */ private $id; public function getId(): int { return $this->id; } /** * {@inheritdoc} */ public function serialize(): string { return serialize([$this->id]); } /** * {@inheritdoc} */ public function unserialize($serialized): void { [$this->id] = unserialize($serialized, ['allowed_classes' => false]); } } ``` My entitymanager is generated like that ```php $paths = [__DIR__ . '/../../src/Entities']; //path to entities $dbParams = [ 'host' => DB_HOST, 'driver' => DB_DRIVER, 'user' => DB_USERNAME, 'password' => DB_PASSWORD, 'dbname' => DB_DATABASE, ]; $config = Setup::createAnnotationMetadataConfiguration($paths, DEV_MODE, null, null, false); $this->entityManager = EntityManager::create($dbParams, $config); ``` Installed packages versions : - doctrine/orm `2.8.2` - symfony/serializer `5.2.4` (from symfony/serializer-pack) Thanks for reading.
admin closed this issue 2026-01-22 15:36:29 +01:00
Author
Owner

@beberlei commented on GitHub (Mar 12, 2021):

Annotations need to be loadable, usually this happens by passing the Composer autoloader to AnnotationRegistry. See here https://www.doctrine-project.org/projects/doctrine-annotations/en/1.10/annotations.html#registering-annotations

@beberlei commented on GitHub (Mar 12, 2021): Annotations need to be loadable, usually this happens by passing the Composer autoloader to `AnnotationRegistry`. See here https://www.doctrine-project.org/projects/doctrine-annotations/en/1.10/annotations.html#registering-annotations
Author
Owner

@ThibautSF commented on GitHub (Mar 12, 2021):

Thanks !

Changed my bootstrap from

require_once __DIR__ . '/vendor/autoload.php';

to

$loader =  require __DIR__ . '/vendor/autoload.php'; //In cli mode (like call .\vendor\bin\doctrine orm:schema-tool:update --dump-sql) require_once returns true 
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
@ThibautSF commented on GitHub (Mar 12, 2021): Thanks ! Changed my bootstrap from ```php require_once __DIR__ . '/vendor/autoload.php'; ``` to ```php $loader = require __DIR__ . '/vendor/autoload.php'; //In cli mode (like call .\vendor\bin\doctrine orm:schema-tool:update --dump-sql) require_once returns true AnnotationRegistry::registerLoader(array($loader, 'loadClass')); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6648