Symfony2 doctrine2 insert record [Semantical Error] The annotation - php

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;

Related

Symfony 3 an exception occurred while executing SELECT NEXTVAL

I get this error when I try to insert something
An exception occurred while executing 'SELECT
NEXTVAL('oficinas_id_seq')':
SQLSTATE[42P01]: Undefined table: 7 ERROR: no existe la relación
«oficinas_id_seq» LINE 1: SELECT NEXTVAL('oficinas_id_seq')
This is my entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Oficinas
*
* #ORM\Table(name="oficinas")
* #ORM\Entity
*/
class Oficinas
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="public.oficinas_id_seq", allocationSize=1, initialValue=1)
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="descripcion", type="string", length=100, nullable=false)
*/
private $descripcion;
/**
* #var \DateTime
*
* #ORM\Column(name="tu", type="datetime", nullable=false)
*/
private $tu;
/**
* #var \DateTime
*
* #ORM\Column(name="ts", type="datetime", nullable=false)
*/
private $ts = 'now()';
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set descripcion
*
* #param string $descripcion
*
* #return Oficinas
*/
public function setDescripcion($descripcion)
{
$this->descripcion = $descripcion;
return $this;
}
/**
* Get descripcion
*
* #return string
*/
public function getDescripcion()
{
return $this->descripcion;
}
}
I´ve test this doctrine commands but didn't work
php bin/console doctrine:mapping:import Bundle annotation php
bin/console generate:doctrine:entities Bundle php bin/console
doctrine:schema:update --force
Any idea?
I just run this command to see the diff between code and DB
php bin/console doctrine:schema:update --dump-sql

Semantic Error whith Symfony annotation and Doctrine

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

Use two entities with the same name in symfony

Hi i need to use two entities with the same name but from a different bundle
Here is how i insert data in my db
use StudentsBundle\Entity\logintrys;
$em = $this->getDoctrine()->getManager();
$start = new logintrys();
$start->setStudentId($username);
$start->setLogintrys($array);
$em = $this->getDoctrine()->getManager();
$em->persist($start);
$em->flush();
And the entity class
<?php
namespace StudentsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* logintrys
*
* #ORM\Table(name="logintrys")
* #ORM\Entity(repositoryClass="StudentsBundle\Repository\logintrysRepository")
*/
class logintrys
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="student_id", type="string", length=20)
*/
private $studentId;
/**
* #var array
*
* #ORM\Column(name="logintrys", type="json_array", nullable=true)
*/
private $logintrys;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set studentId
*
* #param string $studentId
*
* #return logintrys
*/
public function setStudentId($studentId)
{
$this->studentId = $studentId;
return $this;
}
/**
* Get studentId
*
* #return string
*/
public function getStudentId()
{
return $this->studentId;
}
/**
* Set logintrys
*
* #param array $logintrys
*
* #return logintrys
*/
public function setLogintrys($logintrys)
{
$this->logintrys = $logintrys;
return $this;
}
/**
* Get logintrys
*
* #return array
*/
public function getLogintrys()
{
return $this->logintrys;
}
}
My second insert query is exactly the same as the first one only now i can not use $start = new logintrys(); because this is the same name as the first
here is my entity in the teachersBundle
<?php
namespace TeachersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* logintrys
*
* #ORM\Table(name="logintrys")
* #ORM\Entity(repositoryClass="TeachersBundle\Repository\logintrysRepository")
*/
class logintrys
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="docent_id", type="string", length=20)
*/
private $docentId;
/**
* #var array
*
* #ORM\Column(name="logintrys", type="json_array", nullable=true)
*/
private $logintrys;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set docentId
*
* #param string $docentId
*
* #return logintrys
*/
public function setDocentId($docentId)
{
$this->docentId = $docentId;
return $this;
}
/**
* Get docentId
*
* #return string
*/
public function getDocentId()
{
return $this->docentId;
}
/**
* Set logintrys
*
* #param array $logintrys
*
* #return logintrys
*/
public function setLogintrys($logintrys)
{
$this->logintrys = $logintrys;
return $this;
}
/**
* Get logintrys
*
* #return array
*/
public function getLogintrys()
{
return $this->logintrys;
}
}
My question is how can i use the logintrys of the StudentsBundle and TeachersBundle in the same script
Here is the error
Cannot use TeachersBundle\Entity\logintrys as logintrys because the name is already in use
Thank you in advance
When you call your entity with:
use StudentsBundle\Entity\logintrys;
you can add an 'as':
use StudentsBundle\Entity\logintrys as StudentLogintrys;
use TeachersBundle\Entity\logintrys as TeacherLogintrys;
http://php.net/manual/en/language.namespaces.importing.php
Then, in your code you can do:
$start = new StudentLogintrys();
$start2 = new TeacherLogintrys();
I think that the better way is to use inheritance in which the main class contains your logintrys member.
because in php you can't do something like that :
use StudentsBundle\Entity\logintrys;
use TeachersBundle\Entity\logintrys;
this example provide an error because script don't know that kind of class you want instantiate.
I think this is a solution (there are always better but it should work) :
class father{
private logintrys
}
class student extends father{
private id;
private studentId;
}
class teacher extends father{
private id;
private docentId;
}
to create one of them :
$start = new father();
$start->setLogintrys($array);
if($start instanceof student) $start->setStudentId($username);

Doctrine annotations try to autoload wrong annotation

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

Doctrine 2: Update Entity Using Table Schema

I am using doctrine 2 within zend framework 2. To generate methods from existing entities using database table, the console command used is:
php doctrine-module orm:generate-entities --generate-annotations="true" --generate-methods="true" module
I have two namespaces Blog and Location
My question is:
1. When I run above code, only blog entities get updated. I want to know why it is behaving like this?
2. If I want to update only a specific entity, how can i do it?
The blog has two entities: Post and cateogry
Category.php
<?php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
* Category
*
* #ORM\Table(name="Categories")
* #ORM\Entity
*/
class Category implements InputFilterAwareInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
protected $inputFilter;
/**
* Get Id
*
* #param integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
Post.php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table(name="posts")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", precision=0, scale=0, nullable=false, unique=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="content", type="text", precision=0, scale=0, nullable=false, unique=false)
*/
private $content;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime", precision=0, scale=0, nullable=false, unique=false)
*/
private $createdDate;
/**
* #var \Blog\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Blog\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=true)
* })
*/
private $category;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* #param string $content
* #return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
* #return Post
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set category
*
* #param \Blog\Entity\Category $category
* #return Post
*/
public function setCategory(\Blog\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Blog\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
The Location has Country.php
<?php
namespace Country\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Country
*
* #ORM\Table(name="countries")
* #ORM\Entity
*/
class Country
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=55, nullable=false)
*/
private $name;
}
OF Course everyone need a function on ZF2 that's turns to possible you generate a single entity. but those times you can't do it just by doctrine-orm-module. When you try to run doctrine in Symphony it's acceptable. I'm using doctrine2 with zf2 having the same issue.
I create a PHP Script that I call
Script to Create a Single Entity:
Choose Entity Name.
Generate All Entities on a Temp Folder.
Exclude All non-needed entities from folder.
Copy Single Entity to "src/$module/Entity" Folder.
Is a hack that i got to make it work property as you need.
Use the --filter option to do this.
php doctrine-module orm:generate-entities --filter="Post" ...

Categories