Problem with import annotations

I am working on a CodeIgniter project in which I use Doctrine2 and the Symfony2 Validator component.

All my Doctrine2 objects use Doctrine\ORM\Mapping , and the object manager recognizes them. My annotation of an object is as follows:

 /** * @Entity(repositoryClass = "UserRepository") * @Table(name = "user") * @HasLifecycleCallbacks() */ 

At this point, I can save objects without any problems. The first problem occurs when I try to use the Symfony2 Validator component. When I try to validate a User object, I get this error:

[Semantic error] The @Entity annotations in the Entity \ User class were never imported. Did you forget to add the expression "use" for this annotation?

The only "fix" for this problem is through use Doctrine\Mapping\Entity , but I have to do this for every annotation used by my entity (Table, Column, ManyToOne, etc.). I'm trying to understand why I need to explicitly use each annotation class instead of being automatically recognized (shouldn't use Doctrine\ORM\Mapping give me access to all classes in this namespace?).

So, I tried use Doctrine\ORM\Mapping as ORM and preceded all my annotations with ORM\ . Example: @ORM\Entity() . Symfony2 validator stops complaining, but now Doctrine2 complains that Class Entity\User is not a valid entity or mapped super class. I have no idea why this method works for Validator, but not Doctrine2. If I run the console command doctrine:orm:info , my User object is not recognized.

Since this is a CodeIgniter application, I automatically download the Symfony2 and Doctrine2 libraries. My startup code is as follows:

 # Symfony2 ClassLoader component require_once __DIR__ . '/application/libraries/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php'; $loader = new \Symfony\Component\ClassLoader\UniversalClassLoader(); $loader->register(); $loader->registerNamespace('Symfony', __DIR__ . '/application/libraries/symfony/src'); $loader->registerNamespace('Doctrine', __DIR__ . '/application/libraries/doctrine/lib'); # Doctrine2 require_once __DIR__ . '/application/libraries/doctrine/lib/Doctrine/Common/Annotations/AnnotationRegistry.php'; use Doctrine\Common\Annotations\AnnotationRegistry; AnnotationRegistry::registerLoader(function($class) use ($loader) { $loader->loadClass($class); $classExists = class_exists($class, false); return $classExists; }); AnnotationRegistry::registerFile(__DIR__ . '/application/libraries/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); 

I don’t care if I should predict everything with ORM\ or not, I just want to find a solution that Symfony2 Validator and Doctrine2 will work with. Any ideas?

+8
source share
6 answers

I had a similar problem when using Silex, Doctrine2 and Symfony-Validator.

The solution was to avoid using SimpleAnnotationsReader and instead use the regular AnnotationReader (see Doctrine \ ORM \ Configuration :: newDefaultAnnotationDriver ), then you can preface each ORM\ object (and, of course, insert use Doctrine\ORM\Mapping as ORM; to every object).

+20
source

I solved this problem by adding the following to the entity file:

 use Doctrine\ORM\Mapping as ORM; 

So now my file looks like this:

 <?php namespace Acme\CustomBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Example * * @ORM\Entity * @ORM\Table(name="example") * */ class Example { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * * @var integer */ private $id; /** * @ORM\Column(type="string", length=125) * * @var string */ private $title; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set title * * @param string $title * * @return Example */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function gettitle() { return $this->title; } } 
+8
source

Add use for each annotation you used in entities . eg:

 use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Table; use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\GeneratedValue; 

But you may not like to use this method. You can write all use statements in one file and just include it in your entities .

+3
source

If you test and your tests fail because of this. Make sure you have the correct startup configured in phpunit settings.

For a very specific scenario, my problem was updating my symfony version, whereas before I had the bootstrap.cache.php file, but in the new version it is app/autoload . I had to change the phpunit.xml configuration file and set boottrap bootstrap="autoload.php"

0
source

I got the same error message "[Semantic error] The" @Entity "annotation in the Entity \ User class was never imported. Perhaps you forgot to add the" use "statement for this annotation?".

The problem was that I had an alias for ORM, and I did not use it for annotation.

I had this: @OneToOne(targetEntity="CompanyProduct")

And it was right: @ORM\OneToOne(targetEntity="CompanyProduct")

0
source

My problem is solved by adding:

use Doctrine\ORM\Mapping\HasLifecycleCallbacks;

0
source

Source: https://habr.com/ru/post/922613/


All Articles