I try to manage my database with doctrine.
I have two entities voiture et modele, I succeeded to added modele after 3h. When I coded my entity voiture it keeps showing that the base is updated and there is no change.
My modele code:
<?php
/**
* Created by PhpStorm.
* User: jamel_pc
* Date: 2016-11-27
* Time: 9:16 PM
*/
namespace ParcBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Modele
* #package ParcBundle\Entity
*
* #ORM\Entity
* #ORM\Table(name="Modele")
*/
class Modele{
/**
*#ORM\Id
*#ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string",length=255)
*
*/
private $Libelle;
/**
* #ORM\Column(type="string",length=255)
*/
private $Pays;
/**
* #ORM\Column(type="string",length=255)
*/
/**
* #return mixed
*/
public function getLibelle()
{
return $this->Libelle;
}
/**
* #param mixed $Libelle
*/
public function setLibelle($Libelle)
{
$this->Libelle = $Libelle;
}
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return mixed
*/
public function getPays()
{
return $this->Pays;
}
/**
* #param mixed $Pays
*/
public function setPays($Pays)
{
$this->Pays = $Pays;
}
}
and my voiture code:
<?php
/**
* Created by PhpStorm.
* User: jamel_pc
* Date: 2016-11-27
* Time: 11:10 PM
*/
/**
* Class voiture
* #package ParcBundle\Entity
*
* #ORM\Entity
* #ORM\Table(name="Voiture")
*/
namespace ParcBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class voiture
{
/**
*#ORM\Id
*#ORM\Column(type="integer")
*/
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string",length=255)
*
*/
private $Serie;
/**
* #ORM\Column(type="datetime",length=255)
*/
private $DateMiseCirculation;
/**
* #ORM\Column(type="string",length=255)
*/
private $Marque;
/**
* #ORM\ManyToOne(targetEntity="ParcBundle\Entity\Modele" )
* #ORM\JoinColumn(name="id", referencedColumnName="id");
*/
private $modele;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return mixed
*/
public function getModele()
{
return $this->modele;
}
/**
* #param mixed $modele
*/
public function setModele($id)
{
$this->modele = $modele;
}
/**
* #return mixed
*/
public function getSerie()
{
return $this->Serie;
}
/**
* #param mixed $Serie
*/
public function setSerie($Serie)
{
$this->Serie = $Serie;
}
/**
* #return mixed
*/
public function getDateMiseCirculation()
{
return $this->DateMiseCirculation;
}
/**
* #param mixed $DateMiseCirculation
*/
public function setDateMiseCirculation($DateMiseCirculation)
{
$this->DateMiseCirculation = $DateMiseCirculation;
}
/**
* #return mixed
*/
public function getMarque()
{
return $this->Marque;
}
/**
* #param mixed $Marque
*/
public function setMarque($Marque)
{
$this->Marque = $Marque;
}
}
This is the error I get :
error : Nothing to update - your database is already in sync with the
current entity metadata.
It might be because you are importing ORM after you have used it for your Entity annotation.
Try putting your Entity annotations below use use Doctrine\ORM\Mapping as ORM like this:
<?php
/**
* Created by PhpStorm.
* User: jamel_pc
* Date: 2016-11-27
* Time: 11:10 PM
*/
namespace ParcBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class voiture
* #package ParcBundle\Entity
*
* #ORM\Entity
* #ORM\Table(name="Voiture")
*/
class voiture
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/.../
}
You have two annotations for id field. Remove one of them:
/**
*#ORM\Id
*#ORM\Column(type="integer")
*/
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
Related
I use the Symfony version 3.4.0 and I try to use this bundle to translate entities : https://github.com/webfactory/WebfactoryPolyglotBundle
So I created two entities ( Film and FilmTranslation ) for the test
Here my Film.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Annotation as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Film
*
* #ORM\Table(name="film")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmRepository")
* #Polyglot\Locale(primary="fr_FR")
*/
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #Polyglot\TranslationCollection
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="Titre", type="string", length=255, unique=true)
* #Polyglot\Translatable
* #var string|TranslatableInterface
*/
private $titre;
/**
* #ORM\OneToMany(targetEntity="FilmTranslation", mappedBy="entity")
* #Polyglot\TranslationCollection
* #var Collection
*/
private $translations;
public function __construct()
{
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
*
* #return Film
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Add translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*
* #return Film
*/
public function addTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations[] = $translation;
return $this;
}
/**
* Remove translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*/
public function removeTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations->removeElement($translation);
}
/**
* Get translations
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTranslations()
{
return $this->translations;
}
}
And here my FilmTranslation.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Entity\BaseTranslation;
/**
* FilmTranslation
*
* #ORM\Table(name="film_translation")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmTranslationRepository")
* #ORM\Table(
* uniqueConstraints = {
* #ORM\UniqueConstraint(columns={"entity_id", "locale"})
* }
* )
*/
class FilmTranslation extends BaseTranslation
{
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255, unique=true)
*/
private $titre;
/**
* #ORM\ManyToOne(targetEntity="Film", inversedBy="translations")
* #var Film
*/
protected $entity;
/**
* Set titre
*
* #param string $titre
*
* #return FilmTranslation
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set locale
*
* #param string $locale
*
* #return FilmTranslation
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* Set entity
*
* #param \AppBundle\Entity\Film $entity
*
* #return FilmTranslation
*/
public function setEntity(\AppBundle\Entity\Film $entity = null)
{
$this->entity = $entity;
return $this;
}
/**
* Get entity
*
* #return \AppBundle\Entity\Film
*/
public function getEntity()
{
return $this->entity;
}
}
I'm able to create a form but when I try to persist and flush I've the following error :
No mapping found for field 'id' on class 'AppBundle\Entity\Film'.
Is something I'm doing wrong ?
So remove #Polyglot\TranslationCollection from $id annotation like this ;)
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
Actually I've generated two entities:
-Preguntas
-Areas
I'm trying to do a many to many relationshio between these two entities.
In my DataBase I already have 3 tables:
-Pregunta (idPregunta,Titulo)
-Area (idArea,Nombre)
-areas_preguntas (idArea, idPregunta)
These are my entities:
Area:
<?php
namespace RelacionesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Area
*
* #ORM\Entity
*
* #ORM\Table(name="area")
*
*/
class Area
{
/**
* #var int
*
* #ORM\Column(name="idArea", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nombre", type="string", length=255)
*/
private $nombre;
/**
* #ORM\ManyToMany(targetEntity="Pregunta", mappedBy="areas")
*/
private $preguntas;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nombre
*
* #param string $nombre
*
* #return Area
*/
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
/**
* Get nombre
*
* #return string
*/
public function getNombre()
{
return $this->nombre;
}
/**
* Constructor
*/
public function __construct()
{
$this->preguntas = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add pregunta
*
* #param \RelacionesBundle\Entity\Pregunta $pregunta
*
* #return Area
*/
public function addPregunta(\RelacionesBundle\Entity\Pregunta $pregunta)
{
$this->preguntas[] = $pregunta;
return $this;
}
/**
* Remove pregunta
*
* #param \RelacionesBundle\Entity\Pregunta $pregunta
*/
public function removePregunta(\RelacionesBundle\Entity\Pregunta $pregunta)
{
$this->preguntas->removeElement($pregunta);
}
/**
* Get preguntas
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPreguntas()
{
return $this->preguntas;
}
public function __toString(){
return $this->nombre;
}
}
Pregunta:
<?php
namespace RelacionesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Pregunta
*
* #ORM\Entity
*
* #ORM\Table(name="pregunta")
*
*/
class Pregunta
{
/**
* #var int
*
* #ORM\Column(name="idPregunta", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $idPregunta;
/**
* #var string
*
* #ORM\Column(name="titulo", type="string", length=255)
*/
private $titulo;
/**
* #ORM\ManyToMany(targetEntity="Area", inversedBy="areas")
* #ORM\JoinTable(name="areas_preguntas")
*/
private $areas;
/**
* Get idPregunta
*
* #return integer
*/
public function getId()
{
return $this->idPregunta;
}
/**
* Set titulo
*
* #param string $titulo
*
* #return Pregunta
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
return $this;
}
/**
* Get titulo
*
* #return string
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* Constructor
*/
public function __construct()
{
$this->areas = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add area
*
* #param \RelacionesBundle\Entity\Area $area
*
* #return Pregunta
*/
public function addArea(\RelacionesBundle\Entity\Area $area)
{
$this->areas[] = $area;
return $this;
}
/**
* Remove area
*
* #param \RelacionesBundle\Entity\Area $area
*/
public function removeArea(\RelacionesBundle\Entity\Area $area)
{
$this->areas->removeElement($area);
}
/**
* Get areas
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getAreas()
{
return $this->areas;
}
public function __toString(){
return $this->titulo;
}
}
Actually everything works fine, the problem is when I submit the Form of Pregunta, I get that error.
Any advice? or what is wrong?
Looks like by default doctrine is looking for field with name id for JoinTable. Try change JoinTable to
/**
* #ORM\JoinTable(name="areas_preguntas",
* joinColumns={#ORM\JoinColumn(name="pregunta_id", referencedColumnName="idPregunta")},
* inverseJoinColumns={#ORMJoinColumn(name="area_id", referencedColumnName="idArea")}
* )
*/
I have a system with two tables and a join table. Reuniao have many Servidor and a Servidor have many Reuniao. For this I have a reuniao table, servidor table and reuniaoservidor table. I want to persist a reuniao with many existing servidor, but I don't want to persist a servidor when a reuniao is persisted. The problem is...
When I try to persist a reuniao doctrine says to me:
"A new entity was found through the relationship 'SistemaIfnmg\Entity\Reuniao#servidores' that was not configured to cascade persist operations for entity: SistemaIfnmg\Entity\ReuniaoServidor#000000006a69473200007f33ddb737e2. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example #ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'SistemaIfnmg\Entity\ReuniaoServidor#__toString()' to get a clue."
The Entyties:
Reuniao.php
namespace SistemaIfnmg\Entity;
use SistemaIfnmg\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="reuniao")
* #ORM\HasLifecycleCallbacks()
*/
class Reuniao
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $titulo;
/**
* #ORM\Column(type="string", length=255)
*/
private $descricao;
/**
* #ORM\Column(type="datetime")
*/
private $data;
/**
* #ORM\OneToMany(targetEntity="SistemaIfnmg\Entity\ReuniaoServidor", mappedBy="reuniaofk")
*/
private $servidores;
public function __construct(){
$this->servidores = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return string
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* #param string $titulo
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
}
/**
* #return string
*/
public function getDescricao()
{
return $this->descricao;
}
/**
* #param string $descricao
*/
public function setDescricao($descricao)
{
$this->descricao = $descricao;
}
/**
* #return string
*/
public function getData()
{
return $this->data;
}
/**
* #param string $data
*/
public function setData($data)
{
$this->data = $data;
}
/**
* Add servidores
* #param \SistemaIfnmg\Entity\ReuniaoServidor $servidor
* #return Reuniao
*/
public function addServidor(\SistemaIfnmg\Entity\ReuniaoServidor $servidor){
$this->servidores[] = $servidor;
}
/**
* Remove servidor
*
* #param \SistemaIfnmg\Entity\ReuniaoServidor $servidor
*/
public function removeServidor(\SistemaIfnmg\Entity\ReuniaoServidor $servidor){
$this->servidores->removeElement($servidor);
}
/**
* Get servidores
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getServidores(){
return $this->servidores;
}
}
?>
Servidor.php
<?php
namespace SistemaIfnmg\Entity;
use SistemaIfnmg\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
* #ORM\Table(name="servidor")
*/
class Servidor
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #var integer $id
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nome", type="string", length=255)
*/
private $nome;
/**
* #var string
*
* #ORM\Column(name="sobrenome", type="string", length=255)
*/
private $sobrenome;
/**
* #var string
*
* #ORM\Column(name="cargo", type="string", length=255)
*/
private $cargo;
/**
* #var string
*
* #ORM\Column(name="matricula", type="string", length=255)
*/
private $matricula;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get nome
*
* #return string
*/
public function getNome()
{
return $this->nome;
}
/**
* Set nome
*
* #param string $nome
*/
public function setNome($nome)
{
$this->nome = $nome;
}
/**
* Get sobrenome
*
* #return string
*/
public function getSobrenome()
{
return $this->sobrenome;
}
/**
* Set sobrenome
* #param string $sobrenome
*/
public function setSobrenome($sobrenome)
{
$this->sobrenome = $sobrenome;
}
/**
* Get cargo
*
* #return string
*/
public function getCargo()
{
return $this->cargo;
}
/**
* Set cargo
*
* #param string $cargo
*/
public function setCargo($cargo)
{
$this->cargo = $cargo;
}
/**
* Get matricula
*
* #return string
*/
public function getMatricula()
{
return $this->matricula;
}
/**
* Set matricula
*
* #param string $matricula
*/
public function setMatricula($matricula)
{
$this->matricula = $matricula;
}
}
?>
ReuniadoServidor.php
<?php
namespace SistemaIfnmg\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ReuniaoServidor
*
* #ORM\Table(name="reuniaoservidor", indexes={#ORM\Index(name="fk_Reuniao_has_Servidor_Reuniao1_idx", columns={"reuniaofk"}), #ORM\Index(name="fk_Reuniao_has_Servidor_Servidor1_idx", columns= {"servidorfk"})})
* #ORM\Entity
*/
class ReuniaoServidor
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="SistemaIfnmg\Entity\Reuniao", inversedBy="servidores", cascade={"persist"})
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="reuniaofk", referencedColumnName="id")
* })
*/
private $reuniaofk;
/**
* #ORM\ManyToOne(targetEntity="SistemaIfnmg\Entity\Servidor", inversedBy="servidores")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="servidorfk", referencedColumnName="id")
* })
*/
private $servidorfk;
/**
* Get id
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get reuniaofk
* #return \SistemaIfnmg\Entity\Reuniao
*/
public function getReuniaofk()
{
return $this->reuniaofk;
}
/**
* Set reuniaofk
* #param \SistemaIfnmg\Entity\Reuniao $reuniaofk
* #return ReuniaoServidor
*/
public function setReuniaofk($reuniaofk)
{
$this->reuniaofk = $reuniaofk;
return $this;
}
/**
* Get servidorfk
* #return \SistemaIfnmg\Entity\Servidor
*/
public function getServidorfk()
{
return $this->servidorfk;
}
/**
* Set servidorfk
* #param \SistemaIfnmg\Entity\Servidor $servidorfk
* #return ReuniaoServidor
*/
public function setServidorfk($servidorfk)
{
$this->servidorfk = $servidorfk;
return $this;
}
}
?>
I've been trying to fix this error for a long time, please, help??
Thanks a lot
You don't need the entity that joins Reuniao to Servidor (the ReuniaoServidor entity) as Doctrine takes care of the join table for you. You just need to specify the join columns in the JoinTable mapping.
In Reuniao.php change the mapping of servidores to:
/**
* #ORM\ManyToMany(targetEntity="SistemaIfnmg\Entity\Servidor")
* #ORM\JoinTable(name="reuniao_servidor",
* joinColumns={#ORM\JoinColumn(name="ruuniao_id", referencedColumnName="id")},
* inverseJoinColumns{#ORM\JoinColumn(name="servidor_id", referencedColumnName="id")}
* )
*/
private $servidores;
also add a constructor that initializes the array for the collection
public function __construct()
{
$this->servidores = new ArrayCollection();
}
See https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-many-unidirectional
I have two entities with many-to-one association mapping.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Table(name="ShopType")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ShopTypeRepository")
*/
class ShopType
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $shtId;
/**
* #ORM\Column(type="string", length=255)
*/
private $shtName;
/**
* #ORM\Column(type="integer")
*/
private $shtCreateuid;
/**
* #ORM\Column(type="DateTime")
*/
private $shtCreatedate;
/**
* #ORM\Column(type="integer")
*/
private $shtModifyuid;
/**
* #ORM\Column(type="DateTime")
*/
private $shtModifydate;
/**
* #ORM\Column(type="integer")
*/
private $shtdelete;
/**
* One Shop Type has Many Shops.
* #OneToMany(targetEntity="Shop", mappedBy="shtId")
*/
private $shids;
// ...
public function __construct() {
$this->shids = new ArrayCollection();
}
/**
* Set shtId
*
* #param string $shtId
* #return ShopType
*/
public function setShtId($shtId)
{
$this->shtId = $shtId;
return $this;
}
/**
* Get shtId
*
* #return integer
*/
public function getshtId()
{
return $this->shtId;
}
/**
* Set shtName
*
* #param string $shtName
* #return ShopType
*/
public function setShtName($shtName)
{
$this->shtName = $shtName;
return $this;
}
/**
* Get shtName
*
* #return string
*/
public function getShtName()
{
return $this->shtName;
}
/**
* Set shtCreateuid
*
* #param integer $shtCreateuid
* #return ShopType
*/
public function setShtCreateuid($shtCreateuid)
{
$this->shtCreateuid = $shtCreateuid;
return $this;
}
/**
* Get shtCreateuid
*
* #return integer
*/
public function getShtCreateuid()
{
return $this->shtCreateuid;
}
/**
* Set shtCreatedate
*
* #param \DateTime $shtCreatedate
* #return ShopType
*/
public function setShtCreatedate($shtCreatedate)
{
$this->shtCreatedate = $shtCreatedate;
return $this;
}
/**
* Get shtCreatedate
*
* #return \DateTime
*/
public function getShtCreatedate()
{
return $this->shtCreatedate;
}
/**
* Set shtModifyuid
*
* #param integer $shtModifyuid
* #return ShopType
*/
public function setShtModifyuid($shtModifyuid)
{
$this->shtModifyuid = $shtModifyuid;
return $this;
}
/**
* Get shtModifyuid
*
* #return integer
*/
public function getShtModifyuid()
{
return $this->shtModifyuid;
}
/**
* Set shtModifydate
*
* #param \DateTime $shtModifydate
* #return ShopType
*/
public function setShtModifydate($shtModifydate)
{
$this->shtModifydate = $shtModifydate;
return $this;
}
/**
* Get shtModifydate
*
* #return \DateTime
*/
public function getShtModifydate()
{
return $this->shtModifydate;
}
/**
* Set shtdelete
*
* #param string $shtdelete
* #return ShopType
*/
public function setShtDelete($shtdelete)
{
$this->shtdelete = $shtdelete;
return $this;
}
/**
* Get shtdelete
*
* #return integer
*/
public function getshtDelete()
{
return $this->shtdelete;
}
}
This is shoptype entity.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="Shop")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ShopRepository")
*/
class Shop
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $shId;
/**
* Many Features have One Product.
* #ManyToOne(targetEntity="ShopType", inversedBy="shids")
* #JoinColumn(name="shtId", referencedColumnName="sht_id")
*/
private $shtId;
When I delete shop type, I want to check it is used in shop. How can I check? My deletion in shop type is not really delete in database, just change the sht_delete flag 0 to 1. So , I want to know shop is used specific shop type, if so, I just show message "this shop type cannot be delete.". Thanks , for your time.
I have a file /src/AppBundle/Entity/Questionnaire.php with 3 Entity classes, where I'm trying to implement Single table inheritance with Doctrine 2 on Symfony 2.7. Questionnaire is a parent abstract class, and there are 2 child classes FirstQuestions and SecondsQuestions that extends Questionnaire. I choosed this model because I need to write data in table in 2 steps. The code of this file is below:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questionnaire
*
* #ORM\Entity
* #ORM\Table(name="questionnaire")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"firstquestions" = "FirstQuestions", "secondquestions" = "SecondQuestions"})
*/
abstract class Questionnaire {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
/**
* FirstQuestions
*/
class FirstQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="firstName", type="string", length=64)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=32)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="birthday", type="date")
*/
private $birthday;
/**
* #var integer
*
* #ORM\Column(name="shoeSize", type="integer")
*/
private $shoeSize;
/**
* Set firstName
*
* #param string $firstName
*
* #return Questionnaire
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
*
* #return Questionnaire
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* #param string $email
*
* #return Questionnaire
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set birthday
*
* #param \DateTime $birthday
*
* #return Questionnaire
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* #return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set shoeSize
*
* #param integer $shoeSize
*
* #return Questionnaire
*/
public function setShoeSize($shoeSize)
{
$this->shoeSize = $shoeSize;
return $this;
}
/**
* Get shoeSize
*
* #return integer
*/
public function getShoeSize()
{
return $this->shoeSize;
}
}
/**
* SecondQuestions
*/
class SecondQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="favoriteIceCream", type="string", length=128)
*/
private $favoriteIceCream;
/**
* #var string
*
* #ORM\Column(name="favoriteSuperHero", type="string", length=32)
*/
private $favoriteSuperHero;
/**
* #var string
*
* #ORM\Column(name="favoriteMovieStar", type="string", length=64)
*/
private $favoriteMovieStar;
/**
* #var \DateTime
*
* #ORM\Column(name="endOfTheWorld", type="date")
*/
private $endOfTheWorld;
/**
* #var string
*
* #ORM\Column(name="superBowlWinner", type="string", length=32)
*/
private $superBowlWinner;
/**
* Set favoriteIceCream
*
* #param string $favoriteIceCream
*
* #return Questionnaire
*/
public function setFavoriteIceCream($favoriteIceCream)
{
$this->favoriteIceCream = $favoriteIceCream;
return $this;
}
/**
* Get favoriteIceCream
*
* #return string
*/
public function getFavoriteIceCream()
{
return $this->favoriteIceCream;
}
/**
* Set favoriteSuperHero
*
* #param string $favoriteSuperHero
*
* #return Questionnaire
*/
public function setFavoriteSuperHero($favoriteSuperHero)
{
$this->favoriteSuperHero = $favoriteSuperHero;
return $this;
}
/**
* Get favoriteSuperHero
*
* #return string
*/
public function getFavoriteSuperHero()
{
return $this->favoriteSuperHero;
}
/**
* Set favoriteMovieStar
*
* #param string $favoriteMovieStar
*
* #return Questionnaire
*/
public function setFavoriteMovieStar($favoriteMovieStar)
{
$this->favoriteMovieStar = $favoriteMovieStar;
return $this;
}
/**
* Get favoriteMovieStar
*
* #return string
*/
public function getFavoriteMovieStar()
{
return $this->favoriteMovieStar;
}
/**
* Set endOfTheWorld
*
* #param \DateTime $endOfTheWorld
*
* #return Questionnaire
*/
public function setEndOfTheWorld($endOfTheWorld)
{
$this->endOfTheWorld = $endOfTheWorld;
return $this;
}
/**
* Get endOfTheWorld
*
* #return \DateTime
*/
public function getEndOfTheWorld()
{
return $this->endOfTheWorld;
}
/**
* Set superBowlWinner
*
* #param string $superBowlWinner
*
* #return Questionnaire
*/
public function setSuperBowlWinner($superBowlWinner)
{
$this->superBowlWinner = $superBowlWinner;
return $this;
}
/**
* Get superBowlWinner
*
* #return string
*/
public function getSuperBowlWinner()
{
return $this->superBowlWinner;
}
}
So the problem is when I'm trying to create object of child class(FirstQuestions or SecondsQuestions) in method of controller, Symfony displays me error "500 Internal Server Error". The code of controller with method is below:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Questionnaire;
use AppBundle\Entity\FirstQuestions;
use AppBundle\Entity\SecondQuestions;
class TestController extends Controller
{
/**
* #Route("/test", name="test")
*/
public function indexAction(Request $request)
{
$item = new FirstQuestions(); // everything works well without this line
return new Response(
'ok'
);
}
}
Maybe I am doing something wrong or didn't set any important annotation. Can anyone help me?
This will be one of those annoying little oversight errors - an extra semi-colon or something somewhere that you're not looking for it. I'm creating this additional answer so that I can give you exactly the code I'm using. Hopefully, you'll be able to cut-and-paste, replace your own files with this new code, and it will magically start working.
First - to prove the point, here's my (modified) output:
Veromo\Bundle\CoreBundle\Entity\FirstQuestions Object
(
[firstName:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[lastName:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[email:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[birthday:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[shoeSize:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[id:Veromo\Bundle\CoreBundle\Entity\Questionnaire:private] =>
)
Which shows that all I'm doing differently to you is using my own dev environment's namespaces.
AppBundle\Entity\Questionnaire.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questionnaire
*
* #ORM\Entity
* #ORM\Table(name="questionnaire")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"questionnaire"="Questionnaire", "firstquestions" = "FirstQuestions", "secondquestions" = "SecondQuestions"})
*/
abstract class Questionnaire {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
AppBundle\Entity\FirstQuestions.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* FirstQuestions
* #ORM\Entity()
*/
class FirstQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="firstName", type="string", length=64)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=32)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="birthday", type="date")
*/
private $birthday;
/**
* #var integer
*
* #ORM\Column(name="shoeSize", type="integer")
*/
private $shoeSize;
/**
* Set firstName
*
* #param string $firstName
*
* #return Questionnaire
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
*
* #return Questionnaire
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* #param string $email
*
* #return Questionnaire
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set birthday
*
* #param \DateTime $birthday
*
* #return Questionnaire
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* #return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set shoeSize
*
* #param integer $shoeSize
*
* #return Questionnaire
*/
public function setShoeSize($shoeSize)
{
$this->shoeSize = $shoeSize;
return $this;
}
/**
* Get shoeSize
*
* #return integer
*/
public function getShoeSize()
{
return $this->shoeSize;
}
}
AppBundle\Entity\SecondQuestions.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SecondQuestions
* #ORM\Entity()
*/
class SecondQuestions extends Questionnaire {
/**
* #var string
*
* #ORM\Column(name="favoriteIceCream", type="string", length=128)
*/
private $favoriteIceCream;
/**
* #var string
*
* #ORM\Column(name="favoriteSuperHero", type="string", length=32)
*/
private $favoriteSuperHero;
/**
* #var string
*
* #ORM\Column(name="favoriteMovieStar", type="string", length=64)
*/
private $favoriteMovieStar;
/**
* #var \DateTime
*
* #ORM\Column(name="endOfTheWorld", type="date")
*/
private $endOfTheWorld;
/**
* #var string
*
* #ORM\Column(name="superBowlWinner", type="string", length=32)
*/
private $superBowlWinner;
/**
* Set favoriteIceCream
*
* #param string $favoriteIceCream
*
* #return Questionnaire
*/
public function setFavoriteIceCream($favoriteIceCream)
{
$this->favoriteIceCream = $favoriteIceCream;
return $this;
}
/**
* Get favoriteIceCream
*
* #return string
*/
public function getFavoriteIceCream()
{
return $this->favoriteIceCream;
}
/**
* Set favoriteSuperHero
*
* #param string $favoriteSuperHero
*
* #return Questionnaire
*/
public function setFavoriteSuperHero($favoriteSuperHero)
{
$this->favoriteSuperHero = $favoriteSuperHero;
return $this;
}
/**
* Get favoriteSuperHero
*
* #return string
*/
public function getFavoriteSuperHero()
{
return $this->favoriteSuperHero;
}
/**
* Set favoriteMovieStar
*
* #param string $favoriteMovieStar
*
* #return Questionnaire
*/
public function setFavoriteMovieStar($favoriteMovieStar)
{
$this->favoriteMovieStar = $favoriteMovieStar;
return $this;
}
/**
* Get favoriteMovieStar
*
* #return string
*/
public function getFavoriteMovieStar()
{
return $this->favoriteMovieStar;
}
/**
* Set endOfTheWorld
*
* #param \DateTime $endOfTheWorld
*
* #return Questionnaire
*/
public function setEndOfTheWorld($endOfTheWorld)
{
$this->endOfTheWorld = $endOfTheWorld;
return $this;
}
/**
* Get endOfTheWorld
*
* #return \DateTime
*/
public function getEndOfTheWorld()
{
return $this->endOfTheWorld;
}
/**
* Set superBowlWinner
*
* #param string $superBowlWinner
*
* #return Questionnaire
*/
public function setSuperBowlWinner($superBowlWinner)
{
$this->superBowlWinner = $superBowlWinner;
return $this;
}
/**
* Get superBowlWinner
*
* #return string
*/
public function getSuperBowlWinner()
{
return $this->superBowlWinner;
}
}
AppBundle\Controller\TestController.php
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Questionnaire;
use AppBundle\Entity\FirstQuestions;
use AppBundle\Entity\SecondQuestions;
class TestController extends Controller
{
/**
* #Route("/test",name="test")
*/
public function indexAction( Request $request )
{
$item = new FirstQuestions();
return new Response(
'<pre>'.print_r( $item, true ).'</pre>'
);
}
}
And just to be sure ...
app\config\routing.yml
test:
resource: "#AppBundle/Controller/TestController.php"
type: annotation
It's GOT to be some stupid, annoying little error that nobody is looking for.
Hope this helps ...
ALL Entity classes which are part of the mapped entity hierarchy need to be specified in the #DiscriminatorMap. So, yes, your annotation is incorrect.
Doctrine Single Table Inheritance
EDIT
You have another annotations error - neither of your subclasses has an #Entity annotation:
/**
* FirstQuestions
* #ORM\Entity()
*/
class FirstQuestions extends Questionnaire {
/**
* SecondQuestions
* #ORM\Entity()
*/
class SecondQuestions extends Questionnaire {
After fixing this I was able to use Doctrine's Schema Update tool to build the tables AND successfully created a FirstQuestions object.