I try to use Symfony 2.6/Doctrine 2 on Ubuntu 14.04 with php5.5.9/mysql5.5. But i get very strange error and couldn't find any solution.
I create very simple entity with doctrine:generate:entity command. Everything is just fine. But when I try to create table with doctrine:schema:update command I get an imposible to fix error :)
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Doctrine\ORM\Mapping\I" in property AppBundle\Entity\Language::$id does not exist, or could not be auto-loaded.
Well, actually it's right. There is nothing such #Doctrine\ORM\Mapping\I.
It's all about #ORM\Id. When I change #ORM\Id, the error also changes. I change it to #ORM\Hello, the error changes as #Doctrine\ORM\Mapping\Hello. But when I change it to #ORM\Isthisreal, the error stand still as #Doctrine\ORM\Mapping\I.
I thing there is some parsing error about case sensitiveness. But couldn't find any sollution. I tried lots of things but nothing changes. Here is my simple entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Language
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\LanguageRepository")
*/
class Language
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var boolean
*
* #ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* #var string
*
* #ORM\Column(name="iso", type="string", length=2)
*/
private $iso;
/**
* Get id
*
* #return integer
*/
public function getid()
{
return $this->id;
}
/**
* Set isActive
*
* #param boolean $isActive
* #return Language
*/
public function setisActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* #return boolean
*/
public function getisActive()
{
return $this->isActive;
}
/**
* Set iso
*
* #param string $iso
* #return Language
*/
public function setiso($iso)
{
$this->iso = $iso;
return $this;
}
/**
* Get iso
*
* #return string
*/
public function getiso()
{
return $this->iso;
}
}
Try run this before doctrine:schema:update
export LC_ALL=C
Related
This question already has answers here:
"Class XXX is not a valid entity or mapped super class" after moving the class in the filesystem
(15 answers)
Closed 3 years ago.
Actually I've generated my entity Ticket from console with the next command:
php app/console doctrine:generate:entity
And it has generated the entity correctly with the respective getters and setters.
namespace RelacionesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Ticket
*
* #ORM\Table(name="ticket")
* #ORM\Entity(repositoryClass="RelacionesBundle\Repository\TicketRepository")
*/
class Ticket
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Ticket
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
*
* #return Ticket
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
}
The problem is when I edit the entity adding another property:
/**
* #var string
*
* #ORM\Column(name="explanation", type="string", length=150)
*/
private $explanation;
When I enter the following command:
php app/console doctrine:generate:entities RelacionesBundle/Entity/Ticket
I get this error:
Class "RelacionesBundle\Entity\Ticket" is not a valid entity or mapped super class.
Any Advice? or what is wrong?
I resolved the same exception by deleting a conflicting autogenerated orm.php file in the bundle's Resources/config/doctrine folder; according to the documentation: "A bundle can accept only one metadata definition format. For example, it's not possible to mix YAML metadata definitions with annotated PHP entity class definitions."
When trying to insert a record to doctrine, records is inserting but i am getting an error :
[Semantical Error] The annotation "#Expose" in property
C2Educate\ToolsBundle\Entity\StudentScores::$id was never imported.
Did you maybe forget to add a "use" statement for this
annotation? (500 Internal Server Error)
Entity :
<?php
namespace C2Educate\ToolsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\SerializerBundle\Annotation\Type;
/**
* C2Educate\ToolsBundle\Entity\StudentScores
*
* #ORM\Table(name="tbl_student_scores")
* #ORM\Entity
*/
class StudentScores {
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="tbl_student_scores_id_seq", allocationSize="1", initialValue="1")
* #Expose
* #Type("integer")
*/
protected $id;
/**
* #var integer $studentId
*
* #ORM\Column(name="student_id", type="integer", nullable=true)
* #Assert\NotBlank()
* #Type("integer")
*/
private $studentId;
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set studentId
*
* #param string $studentId
*/
public function setStudentId($studentId) {
$this->studentId = $studentId;
}
/**
* Get studentId
*
* #return string
*/
public function getStudentId() {
return $this->studentId;
}
}
Controller Script :
$em = $this->getDoctrine()->getEntityManager();
$sc = new StudentScores();
$sc->setScore((!empty($test->composite) ? $test->composite : 0));
$sc->setTestDate($test->test_date);
$sc->setcreatedBy($loggedinUser);
$sc->setCreatedAt($date);
$sc->setupdatedBy($loggedinUser);
$sc->setUpdatedAt($date);
$sc->setStudentId($returnID);
$em->persist($sc);
$em->flush();
You forgot to add use statements as it says in error description. Try this
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
I use Symfony 3.2. I have two related entities:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* #ORM\Table(name="user", options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"}, indexes={ #Index(name="idx_email", columns={"email"}) })
* #UniqueEntity(fields={"email"}, message="Пользователь с данным email'ом существует.", groups={"Registration"})
* #ORM\HasLifecycleCallbacks()
*/
class User
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
/**
*
* #ORM\OneToMany(targetEntity="AppBundle\Entity\VisitedPage", mappedBy="user")
*/
private $visitedPages;
public function __construct()
{
$this->visitedPages = new ArrayCollection();
}
public function addVisitedPage(VisitedPage $visitedPage)
{
$this->visitedPages[] = $visitedPage;
return $this;
}
/**
* Remove visitedPage
*
* #param \AppBundle\Entity\VisitedPage $visitedPage
*/
public function removeVisitedPage(\AppBundle\Entity\VisitedPage $visitedPage)
{
$this->visitedPages->removeElement($visitedPage);
}
/**
* Get visitedPages
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getVisitedPages()
{
return $this->visitedPages;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* VisitedPage
*
* #ORM\Table(name="visited_page")
* #ORM\Entity(repositoryClass="AppBundle\Repository\VisitedPageRepository")
*/
class VisitedPage
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="visitedPages")
*/
private $user;
/**
* #var string
* #ORM\Column(name="page", type="text")
*/
private $page;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* #param \AppBundle\Entity\User $user
*
* #return VisitedPage
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set page
*
* #param string $page
*
* #return VisitedPage
*/
public function setPage($page)
{
$this->page = $page;
return $this;
}
/**
* Get page
*
* #return string
*/
public function getPage()
{
return $this->page;
}
}
When i try call VisitedPage::getUser() (i mean $visitedPage->getUser()) i get object type of user.
But when i call method User::getVisitedPages() i get null.
Method User::addVisitedPage($page) works and relations are saved.
How can i decide this problem?
//Some text to pass validation
When you add Visited Page from user try this:
class User
{
//......
public function addVisitedPage(VisitedPage $visitedPage)
{
$visitedPage->setUser($this);
$this->visitedPages->add($visitedPage);
return $this;
}
}
also I think you will need to add cascade={"persist"} to your private $visitedPages; inside of User object:
/**
*
* #ORM\OneToMany(targetEntity="AppBundle\Entity\VisitedPage", mappedBy="user", cascade={"persist"})
*/
private $visitedPages;
I fixed the issue by running this command - bin/console doctrine:cache:clear-metadata
You should run
php app/console cache:clear --env=prod
or
php app/console cache:clear --env=dev
depending upon env you are working
I need to add a column to a database with Doctrine migration but the console returns me that:
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Doctrine\ORM\Mapping\progetto_id" in property Bs\PlutoBundle\Entity\ProgettoPaesiOCM::$progetto_id does not exist, or could not be auto-loaded.
The code is:
<?php
namespace Bs\PlutoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* PaesiOCM
*
* #ORM\Table(name="progetto_paesiocm")
* #ORM\Entity
* #Serializer\ExclusionPolicy("all")
*/
class ProgettoPaesiOCM
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="progetto_id", type="integer")
* #ORM\progetto_id
*/
protected $progetto_id;
/**
* #var integer
*
* #ORM\Column(name="paesiocm_id", type="integer")
* #ORM\Column paesiocm_id
*/
protected $paesiocm_id;
///////////////////////////////////////////////////////////////////
/**
* Get progetto_id
* #return int
*/
public function getProgettoId()
{
return $this->progetto_id;
}
/**
* Set progetto_id
*
* #param int $progetto
* #return ProgettoPaesiOCM
*/
public function setProgettoId($progetto)
{
$this->progetto_id = $progetto;
return $this;
}
///////////////////////////////////////////////////////////////////////
/**
* Get paesiocm_id
* #return int
*/
public function getPaesiOcmId()
{
return $this->paesiocm_id;
}
/**
* Set paesiocm_id
*
* #param int $paesi
* #return ProgettoPaesiOCM
*/
public function setPaesiOcmId($paesi)
{
$this->paesiocm_id = $paesi;
return $this;
}
//////////////////////////////////////////////////////////////////////
/**
* Constructor
*/
public function __construct()
{
$this->progettoPaesiOCM = new \Doctrine\Common\Collections\ArrayCollection();
}
}
You should delete this line
* #ORM\progetto_id
And this line
* #ORM\Column paesiocm_id
I'm usual working with symfony from the 2.1 or 2.2 versions.
Today i started a new project on the 2.3 and i'm encountering problems to create my custom entity repository.
My entity is:
<?php
namespace Acme\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* AnnualProduction
*
* #ORM\Table(name="Annual_Production")
* #ORM\Entity(repositoryClass="Acme\MyBundle\Entity\AnnualproductionRepository")
*/
class AnnualProduction
{
/**
* #var string
*
* #ORM\Column(name="device_address", type="string", length=45, nullable=true)
*/
private $deviceAddress;
/**
* #var integer
*
* #ORM\Column(name="mese_1", type="integer", nullable=true)
*/
private $mese1;
/**
* #var integer
*
* #ORM\Column(name="mese_2", type="integer", nullable=true)
*/
SOME MISSING VAR SET AND GET
/**
* #var string
*
* #ORM\Column(name="sens_id", type="string", length=45)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $sensId;
/**
* #var \DateTime
*
* #ORM\Column(name="AAAA", type="date")
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $aaaa;
/**
* Set deviceAddress
*
* #param string $deviceAddress
* #return AnnualProduction
*/
public function setDeviceAddress($deviceAddress)
{
$this->deviceAddress = $deviceAddress;
return $this;
}
/**
* Get deviceAddress
*
* #return string
*/
public function getDeviceAddress()
{
return $this->deviceAddress;
}
/**
* Set mese1
*
* #param integer $mese1
* #return AnnualProduction
*/
public function setMese1($mese1)
{
$this->mese1 = $mese1;
return $this;
}
/**
* Get mese1
*
* #return integer
*/
public function getMese1()
{
return $this->mese1;
}
/**
* Set sensId
*
* #param string $sensId
* #return AnnualProduction
*/
public function setSensId($sensId)
{
$this->sensId = $sensId;
return $this;
}
/**
* Get sensId
*
* #return string
*/
public function getSensId()
{
return $this->sensId;
}
/**
* Set aaaa
*
* #param \DateTime $aaaa
* #return AnnualProduction
*/
public function setAaaa($aaaa)
{
$this->aaaa = $aaaa;
return $this;
}
/**
* Get aaaa
*
* #return \DateTime
*/
public function getAaaa()
{
return $this->aaaa;
}
}
I dont write all variable and get and sets functions.
I've created a repository file: Acme\MyBundle\Entity\AnnualproductionRepository.php
The code for the repository file is the following:
<?php
namespace Acme\MyBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use Acme\MyBundle\Entity\AnnualProduction;
class AnnualproductionRepository extends EntityRepository
{
public function findByYearMonthDay($anno, $mese, $giorno, $sensId)
{
$query = $this->getEntityManager()
->createQuery(" SOME QUERY HERE")->setParameters(array(SOME PARAMETERS HERE));
return $query->getSingleResult();
}
}
I call the repository in one of my controller, with the following code:
<?php
namespace Acme\MyBundle\Controller;
use Acme\MyBundle\Entity\AnnualProduction;
use Acme\MyBundle\Entity;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints\Date;
class DataController extends Controller{
public function indexUserAction(){
*
*
*
*
*
$DailyProduction=$DailyProduction+$em->getRepository('AcmeMyBundle:AnnualProduction')->findByYearMonthDay($year, $month, $day, $productionSensor);
*
*
*
*
}
}
But i get this error like the repository doesnt exist and the controller get the function name like a default findBy* on one of the Entity attributes.
ERROR:
*> Entity 'Acme\MyBundle\Entity\AnnualProduction' has no field
'yearMonthDay'. You can therefore not call 'findByYearMonthDay' on the
entities' repository***
Have you some advise to solve this problem? the code seems to be identical to the one i usualy add to include custom entity repository in symfony 2.2 but for some reason it refuse to work.
Tx for your time and Help.
Problem Solved, the fact was that the entity.orm.xml files, stored in /src/acme/myBundle/config/doctrine, have an hight priority over the entity files, so every modification to the entity that i was doing wasent readed.
SOLUTION: Delete all the entity.orm.xml files after done the annotation ed the generation of the entity via terminal php command.
The namespace for your repository is wrong. You should have Acme\MyBundle\Entity\Repository for a namespace instead. The path to your file ought to then be Acme/MyBundle/Entity/Repository/AnnualProduction.
You should also let doctrine generate all your entities for you via
php app/console doctrine:generate:entities Acme
You will then see a folder called Repositories in your Entities folder and thats where all the entities need to be stored.