Trouble with importing annotations

前端 未结 6 1736
闹比i
闹比i 2021-01-11 15:14

I\'m working on a CodeIgniter project in which I\'m using Doctrine2 and the Symfony2 Validator component.

All my Doctrine2 entities use Doctrine\\ORM\\Mapping

相关标签:
6条回答
  • 2021-01-11 15:58

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

    The solution was to avoid using the SimpleAnnotationsReader and instead using the normal AnnotationReader (have a look at Doctrine\ORM\Configuration::newDefaultAnnotationDriver). You can then preface every entity with ORM\ (and of course inserting use Doctrine\ORM\Mapping as ORM; in every entity).

    0 讨论(0)
  • 2021-01-11 16:01

    I resolved this issue by adding the following to my entity file:

    use Doctrine\ORM\Mapping as ORM;
    

    So my file now looks like:

    <?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;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-11 16:02

    I was getting the same error message "[Semantical Error] The annotation "@Entity" in class Entity\User was never imported. Did you maybe forget to add a "use" statement for this annotation?".

    The problem was that I had alias for ORM and did not used it for the annotation.

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

    And correct was this: @ORM\OneToOne(targetEntity="CompanyProduct")

    0 讨论(0)
  • 2021-01-11 16:03

    If you are testing and your tests fail because of this. Make sure you have the right autoload configured in your phpunit settings.

    For a very specific scenario, my problem was upgrading my symfony version whereas before i had the bootstrap.cache.php file, but on a new version, it is app/autoload. I had to change my phpunit.xml config file and set my bootstrap option to that file bootstrap="autoload.php"

    0 讨论(0)
  • 2021-01-11 16:07

    My problem solved by adding:

    use Doctrine\ORM\Mapping\HasLifecycleCallbacks;

    0 讨论(0)
  • 2021-01-11 16:14

    Add use statements for every annotation you have used in your entities. for example:

    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 interesting to use this method. You can write all use statements to one file and just include it in your entities.

    0 讨论(0)
提交回复
热议问题