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
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).
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;
}
}
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")
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"
My problem solved by adding:
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
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
.