doctrine one-to-one relation mapping - php

i have two entities that i would like to map them so that when i generate crud for each entity when i want to make a new insert using Map entity to be able to select based on id from Board entity.
I've been trying to make the mapping yet .. i didn't manage not sure if i did it right or not because in mysql i don't see a foreign key after sql generation.
Board.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Board
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\BoardRepository")
*/
class Board
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\ManyToOne(targetEntity="Board")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="BoardId")
* })
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="PropertyName", type="string", length=255)
*/
private $propertyName;
/**
* #var string
*
* #ORM\Column(name="PropertyDescription", type="string", length=255)
*/
private $propertyDescription;
/**
* #var string
*
* #ORM\Column(name="PropertyPicture", type="string", length=255)
*/
private $propertyPicture;
/**
* #var string
*
* #ORM\Column(name="PropertyGlyphonic", type="string", length=255)
*/
private $propertyGlyphonic;
/**
* #var string
*
* #ORM\Column(name="PropertyPrice", type="string", length=255)
*/
private $propertyPrice;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set propertyName
*
* #param string $propertyName
* #return Board
*/
public function setPropertyName($propertyName)
{
$this->propertyName = $propertyName;
return $this;
}
/**
* Get propertyName
*
* #return string
*/
public function getPropertyName()
{
return $this->propertyName;
}
/**
* Set propertyDescription
*
* #param string $propertyDescription
* #return Board
*/
public function setPropertyDescription($propertyDescription)
{
$this->propertyDescription = $propertyDescription;
return $this;
}
/**
* Get propertyDescription
*
* #return string
*/
public function getPropertyDescription()
{
return $this->propertyDescription;
}
/**
* Set propertyPicture
*
* #param string $propertyPicture
* #return Board
*/
public function setPropertyPicture($propertyPicture)
{
$this->propertyPicture = $propertyPicture;
return $this;
}
/**
* Get propertyPicture
*
* #return string
*/
public function getPropertyPicture()
{
return $this->propertyPicture;
}
/**
* Set propertyGlyphonic
*
* #param string $propertyGlyphonic
* #return Board
*/
public function setPropertyGlyphonic($propertyGlyphonic)
{
$this->propertyGlyphonic = $propertyGlyphonic;
return $this;
}
/**
* Get propertyGlyphonic
*
* #return string
*/
public function getPropertyGlyphonic()
{
return $this->propertyGlyphonic;
}
/**
* Set propertyPrice
*
* #param string $propertyPrice
* #return Board
*/
public function setPropertyPrice($propertyPrice)
{
$this->propertyPrice = $propertyPrice;
return $this;
}
/**
* Get propertyPrice
*
* #return string
*/
public function getPropertyPrice()
{
return $this->propertyPrice;
}
}
Map.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Map
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\MapRepository")
*/
class Map
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
* #ORM\Column(name="BoardId", type="integer")
*/
private $boardId;
/**
* #var string
*
* #ORM\Column(name="X", type="string", length=255)
*/
private $x;
/**
* #var string
*
* #ORM\Column(name="Y", type="string", length=255)
*/
private $y;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set boardId
*
* #param integer $boardId
* #return Map
*/
public function setBoardId($boardId)
{
$this->boardId = $boardId;
return $this;
}
/**
* Get boardId
*
* #return integer
*/
public function getBoardId()
{
return $this->boardId;
}
/**
* Set x
*
* #param string $x
* #return Map
*/
public function setX($x)
{
$this->x = $x;
return $this;
}
/**
* Get x
*
* #return string
*/
public function getX()
{
return $this->x;
}
/**
* Set y
*
* #param string $y
* #return Map
*/
public function setY($y)
{
$this->y = $y;
return $this;
}
/**
* Get y
*
* #return string
*/
public function getY()
{
return $this->y;
}
}

It seams like you haven't read the manual. It is great documentation on both Symfony.com and Doctrine-orm.readthedocs.org.
Read this article (http://symfony.com/doc/current/book/doctrine.html) and use this one as reference (http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html) and you will find the answer to this and many future questions.
What you basically need to do is:
class Board {
/**
* #ORM\ManyToOne(targetEntity="Map")
*/
private $map;
}

Related

Symfony2 - Retrieve Relational Data From Doctrine

When I try to get data like this, I got missing data:
$this->em->getRepository("PollBundle:Poll")->findAll();
Here are the results that I got: http://i.imgur.com/f0M54og.png
The system is getting poll's data however nothing to show about frequency as you can see. What's wrong?
PollFrequency Entity is like that as following:
namespace PollBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* PollFrequency
*
* #ORM\Table(name="poll_frequency")
* #ORM\Entity
*/
class PollFrequency
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="Poll", mappedBy="poll_frequency")
*/
private $polls;
/**
* #var string
* #ORM\Column(name="frequency", type="string")
*/
private $frequency;
/**
* #var string
* #ORM\Column(name="description", type="string")
*/
private $description;
public function __construct()
{
$this->polls = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set frequency
*
* #param string $frequency
* #return PollFrequency
*/
public function setFrequency($frequency)
{
$this->frequency = $frequency;
return $this;
}
/**
* Get frequency
*
* #return string
*/
public function getFrequency()
{
return $this->frequency;
}
/**
* Set description
*
* #param string $description
* #return PollFrequency
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Add polls
*
* #param \PollBundle\Entity\Poll $polls
* #return PollFrequency
*/
public function addPoll(\PollBundle\Entity\Poll $polls)
{
$this->polls[] = $polls;
return $this;
}
/**
* Remove polls
*
* #param \PollBundle\Entity\Poll $polls
*/
public function removePoll(\PollBundle\Entity\Poll $polls)
{
$this->polls->removeElement($polls);
}
/**
* Get polls
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPolls()
{
return $this->polls;
}
}
Poll Entity is also here as following:
<?php
namespace PollBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Poll
*
* #ORM\Table(name="poll")
* #ORM\Entity
*/
class Poll
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #ORM\Column(name="name", type="string", nullable=false, length=255)
*/
private $name;
/**
* #var boolean
* #ORM\Column(name="pageload", nullable=false, type="boolean")
*/
private $pageload;
/**
* #var integer
* #ORM\Column(name="loadafter", nullable=false, type="integer")
*/
private $loadafter;
/**
* #var boolean
* #ORM\Column(name="exitload", nullable=false, type="boolean")
*/
private $exitload;
/**
* #var integer
* #ORM\Column(name="viewcount", nullable=false, type="integer")
*/
private $viewcount;
/**
* #var integer
* #ORM\Column(name="status", nullable=false, type="integer")
*/
private $status;
/**
* #var integer
* #ORM\Column(name="user_id", nullable=false, type="integer")
*/
private $user_id;
/**
* #ORM\OneToMany(targetEntity="PollFrequency", mappedBy="frequency")
* #ORM\JoinColumn(name="frequency_id", referencedColumnName="id")
*/
private $frequency;
/**
* #var \DateTime
* #ORM\Column(name="createdate", nullable=false, type="datetime")
*/
private $createdate;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Poll
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set pageload
*
* #param boolean $pageload
* #return Poll
*/
public function setPageload($pageload)
{
$this->pageload = $pageload;
return $this;
}
/**
* Get pageload
*
* #return boolean
*/
public function getPageload()
{
return $this->pageload;
}
/**
* Set loadafter
*
* #param integer $loadafter
* #return Poll
*/
public function setLoadafter($loadafter)
{
$this->loadafter = $loadafter;
return $this;
}
/**
* Get loadafter
*
* #return integer
*/
public function getLoadafter()
{
return $this->loadafter;
}
/**
* Set exitload
*
* #param boolean $exitload
* #return Poll
*/
public function setExitload($exitload)
{
$this->exitload = $exitload;
return $this;
}
/**
* Get exitload
*
* #return boolean
*/
public function getExitload()
{
return $this->exitload;
}
/**
* Set viewcount
*
* #param integer $viewcount
* #return Poll
*/
public function setViewcount($viewcount)
{
$this->viewcount = $viewcount;
return $this;
}
/**
* Get viewcount
*
* #return integer
*/
public function getViewcount()
{
return $this->viewcount;
}
/**
* Set status
*
* #param integer $status
* #return Poll
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set user_id
*
* #param integer $userId
* #return Poll
*/
public function setUserId($userId)
{
$this->user_id = $userId;
return $this;
}
/**
* Get user_id
*
* #return integer
*/
public function getUserId()
{
return $this->user_id;
}
/**
* Set createdate
*
* #param \DateTime $createdate
* #return Poll
*/
public function setCreatedate($createdate)
{
$this->createdate = $createdate;
return $this;
}
/**
* Get createdate
*
* #return \DateTime
*/
public function getCreatedate()
{
return $this->createdate;
}
/**
* Set frequency
*
* #param \PollBundle\Entity\PollFrequency $frequency
* #return Poll
*/
public function setFrequency(\PollBundle\Entity\PollFrequency $frequency = null)
{
$this->frequency = $frequency;
return $this;
}
/**
* Get frequency
*
* #return \PollBundle\Entity\PollFrequency
*/
public function getFrequency()
{
return $this->frequency;
}
}
Happy Coding to All !
I solved like that. If you need solution. that's it as following:
/**
* #ORM\ManyToOne(targetEntity="PollFrequency", fetch="EAGER")
* #ORM\JoinColumn(name="frequency_id", referencedColumnName="id")
*/
private $frequency;
And also I removed that part of PollFrequency as following:
/**
* #ORM\OneToMany(targetEntity="Poll", mappedBy="poll_frequency")
*/
private $polls;
and everything that related to $polls.
Happy Coding to All!

#ORM\PrePersist is not allowed to be declared on property

i have been learning zend framework for the past days now. I have encountered this error when i tried to run the site:
Annotation #ORM\PrePersist is not allowed to be declared on property Post\Entity\Post::$alterado. You may only use this annotation on these code elements: METHOD.
heres my Post.php file:
<?php
namespace Post\Entity;
use Application\Entity\AbstractEntity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table(name="post")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
* #ORM\Entity(repositoryClass="Post\Entity\PostRepository")
*/
class Post extends AbstractEntity
{
/**
* #var integer
* #ORM\Id
* #ORM\Column(name="id", type="integer", nullable=false)
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="titulo", type="string", length=80, nullable=false)
*/
private $titulo;
/**
* #var string
*
* #ORM\Column(name="descricao", type="string", length=150, nullable=false)
*/
private $descricao;
/**
* #var string
*
* #ORM\Column(name="texto", type="text", nullable=false)
*/
private $texto;
/**
* #var \DateTime
*
* #ORM\Column(name="cadastro", type="datetime", nullable=false)
*/
private $cadastro;
/**
* #var \DateTime
* #ORM\PrePersist
* #ORM\Column(name="alterado", type="datetime", nullable=false)
*/
private $alterado;
/**
* #var boolean
* #ORM\PosUpdate
* #ORM\Column(name="ativo", type="boolean", nullable=false)
*/
private $ativo = '0';
/**
* #var \Categoria\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Categoria\Entity\Category")
* #ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
*/
private $categoriaId;
/**
* Set id
*
* #param integer $id
*
* #return Post
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titulo
*
* #param string $titulo
*
* #return Post
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
return $this;
}
/**
* Get titulo
*
* #return string
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* Set descricao
*
* #param string $descricao
*
* #return Post
*/
public function setDescricao($descricao)
{
$this->descricao = $descricao;
return $this;
}
/**
* Get descricao
*
* #return string
*/
public function getDescricao()
{
return $this->descricao;
}
/**
* Set texto
*
* #param string $texto
*
* #return Post
*/
public function setTexto($texto)
{
$this->texto = $texto;
return $this;
}
/**
* Get texto
*
* #return string
*/
public function getTexto()
{
return $this->texto;
}
/**
* Set cadastro
*
* #param \DateTime $cadastro
*
* #return Post
*/
public function setCadastro($cadastro)
{
$this->cadastro = $cadastro;
return $this;
}
/**
* Get cadastro
*
* #return \DateTime
*/
public function getCadastro()
{
return $this->cadastro;
}
/**
* Set alterado
*
* #param \DateTime $alterado
*
* #return Post
*/
public function setAlterado($alterado)
{
$this->alterado = $alterado;
return $this;
}
/**
* Get alterado
*
* #return \DateTime
*/
public function getAlterado()
{
return $this->alterado;
}
/**
* Set ativo
*
* #param boolean $ativo
*
* #return Post
*/
public function setAtivo($ativo)
{
$this->ativo = $ativo;
return $this;
}
/**
* Get ativo
*
* #return boolean
*/
public function getAtivo()
{
return $this->ativo;
}
/**
* Set categoriaId
*
* #param \Categoria\Entity\Category $categoriaId
*
* #return Post
*/
public function setCategoriaId(\Categoria\Entity\Category $categoriaId = null)
{
$this->categoriaId = $categoriaId;
return $this;
}
/**
* Get categoriaId
*
* #return \Categoria\Entity\Category
*/
public function getCategoriaId()
{
return $this->categoriaId;
}
}
Am i missing any "use" statement on the file?
Any help is apreciated, thank you for your atention :)
I fixed it, i should have call #ORM\PrePersist and #ORM\PosUpdate on the method. :)

Semantical Error line 0, col 140 near 'Q INNER JOIN': Error: Class Search\serachBundle\Entity\Person has no association named Qualification

what's the wrong in this code
i want to create query with multi join but unfortunately this error displayed
[this is my SearchController controller]
class SearchController extends Controller {
public function indexAction() {
$em = $this->get('doctrine.orm.entity_manager');
$users = $em
->createQueryBuilder('P')
->select('P.pname, Q.qname,P.gender,P.phone,P.yearsOfExperience,P.graduationYear,c.cname')
->add('from', 'serachBundle:Person P')
->Join('P.Qualification', 'Q')
->Join('Q.Category', 'c')
->where('P.qualificationId=Q.id')
->andwhere('Q.categoryId=c.id')
->getQuery();
$user = $users->getResult();
print_r($user);
//qualification
$Qlist = $this->getDoctrine()
->getRepository('serachBundle:qualification')
->findAll();
//category
$Catlist = $this->getDoctrine()
->getRepository('serachBundle:category')
->findAll();
return $this->render('serachBundle:Default:Search.html.twig', array('user' => $user, 'Qlist' => $Qlist, 'Catlist' => $Catlist));
}
this my person entity
<?php
namespace Search\serachBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Person
*
* #ORM\Table(name="person", indexes={#ORM\Index(name="companyId", columns={"companyId", "qualificationId"}), #ORM\Index(name="qualificationId", columns={"qualificationId"}), #ORM\Index(name="IDX_34DCD1762480E723", columns={"companyId"})})
* #ORM\Entity
*/
class Person
{
/**
* #var string
*
* #ORM\Column(name="pname", type="string", length=255, nullable=false)
*/
private $pname;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255, nullable=false)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=255, nullable=false)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="gender", type="string", length=255, nullable=false)
*/
private $gender;
/**
* #var integer
*
* #ORM\Column(name="yearsOfExperience", type="integer", nullable=false)
*/
private $yearsofexperience;
/**
* #var integer
*
* #ORM\Column(name="graduationYear", type="integer", nullable=false)
*/
private $graduationyear;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Search\serachBundle\Entity\Company
*
* #ORM\ManyToOne(targetEntity="Search\serachBundle\Entity\Company")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="companyId", referencedColumnName="id")
* })
*/
private $companyid;
/**
* #var \Search\serachBundle\Entity\Qualification
*
* #ORM\ManyToOne(targetEntity="Search\serachBundle\Entity\Qualification")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="qualificationId", referencedColumnName="id")
* })
*/
private $qualificationid;
/**
* Set pname
*
* #param string $pname
* #return Person
*/
public function setPname($pname)
{
$this->pname = $pname;
return $this;
}
/**
* Get pname
*
* #return string
*/
public function getPname()
{
return $this->pname;
}
/**
* Set address
*
* #param string $address
* #return Person
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set phone
*
* #param string $phone
* #return Person
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* #return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set gender
*
* #param string $gender
* #return Person
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* #return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set yearsofexperience
*
* #param integer $yearsofexperience
* #return Person
*/
public function setYearsofexperience($yearsofexperience)
{
$this->yearsofexperience = $yearsofexperience;
return $this;
}
/**
* Get yearsofexperience
*
* #return integer
*/
public function getYearsofexperience()
{
return $this->yearsofexperience;
}
/**
* Set graduationyear
*
* #param integer $graduationyear
* #return Person
*/
public function setGraduationyear($graduationyear)
{
$this->graduationyear = $graduationyear;
return $this;
}
/**
* Get graduationyear
*
* #return integer
*/
public function getGraduationyear()
{
return $this->graduationyear;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set companyid
*
* #param \Search\serachBundle\Entity\Company $companyid
* #return Person
*/
public function setCompanyid(\Search\serachBundle\Entity\Company $companyid = null)
{
$this->companyid = $companyid;
return $this;
}
/**
* Get companyid
*
* #return \Search\serachBundle\Entity\Company
*/
public function getCompanyid()
{
return $this->companyid;
}
/**
* Set qualificationid
*
* #param \Search\serachBundle\Entity\Qualification $qualificationid
* #return Person
*/
public function setQualificationid(\Search\serachBundle\Entity\Qualification $qualificationid = null)
{
$this->qualificationid = $qualificationid;
return $this;
}
/**
* Get qualificationid
*
* #return \Search\serachBundle\Entity\Qualification
*/
public function getQualificationid()
{
return $this->qualificationid;
}
}
(Entities are objects with identity. Their identity has a conceptual meaning inside your domain. In a CMS application each article has a unique id. You can uniquely identify each article by that id.)
this my Qualification entity
<?php
namespace Search\serachBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Qualification
*
* #ORM\Table(name="qualification", indexes={#ORM\Index(name="categoryId", columns={"categoryId"})})
* #ORM\Entity
*/
class Qualification
{
/**
* #var string
*
* #ORM\Column(name="qname", type="string", length=255, nullable=false)
*/
private $qname;
/**
* #var string
*
* #ORM\Column(name="specialty", type="string", length=255, nullable=false)
*/
private $specialty;
/**
* #var string
*
* #ORM\Column(name="qualifDesc", type="string", length=255, nullable=false)
*/
private $qualifdesc;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Search\serachBundle\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Search\serachBundle\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="categoryId", referencedColumnName="id")
* })
*/
private $categoryid;
/**
* Set qname
*
* #param string $qname
* #return Qualification
*/
public function setQname($qname)
{
$this->qname = $qname;
return $this;
}
/**
* Get qname
*
* #return string
*/
public function getQname()
{
return $this->qname;
}
/**
* Set specialty
*
* #param string $specialty
* #return Qualification
*/
public function setSpecialty($specialty)
{
$this->specialty = $specialty;
return $this;
}
/**
* Get specialty
*
* #return string
*/
public function getSpecialty()
{
return $this->specialty;
}
/**
* Set qualifdesc
*
* #param string $qualifdesc
* #return Qualification
*/
public function setQualifdesc($qualifdesc)
{
$this->qualifdesc = $qualifdesc;
return $this;
}
/**
* Get qualifdesc
*
* #return string
*/
public function getQualifdesc()
{
return $this->qualifdesc;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set categoryid
*
* #param \Search\serachBundle\Entity\Category $categoryid
* #return Qualification
*/
public function setCategoryid(\Search\serachBundle\Entity\Category $categoryid = null)
{
$this->categoryid = $categoryid;
return $this;
}
/**
* Get categoryid
*
* #return \Search\serachBundle\Entity\Category
*/
public function getCategoryid()
{
return $this->categoryid;
}
}
Thanks in advance if anyone can solve this. It would be a really great help
You named the relations as $qualificationid, so you should make your query something like this:
->Join('P.qualificationid', 'Q')
->Join('Q.categoryid', 'c')
Remember to make all queries inside repositories classes. And please, try to rename your bundle from serach to search :-D

Error in Symfony 2 when create object of child entity class

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.

Why command generate:doctrine:entities don't generate methods in these classes? (reverse engineering)

(I'm a beginner)
I would like to automatically generate methods for related models (OneToMany and ManyToOne). Im doing it following docs http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
I added following properties and annotations to the models (I give here only two classes related by ManyToOne as a example) and run generate:doctrine:entities command with no errors and no results. I expected methods like __construct and addXXX() in Kategoria class with OnoToMany relation.
This is fragment I added to Kategoria class.
/**
* #ORM\OneToMany(targetEntity="Ksiazka", mappedBy="kategoria")
*/
protected $ksiazki;
This is entire Kategoria class with OneToMany relation.
#Kategoria.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Kategoria
*
* #ORM\Table(name="kategoria")
* #ORM\Entity
*/
class Kategoria
{
/**
* #var string
*
* #ORM\Column(name="nazwa", type="string", length=45, nullable=true)
*/
private $nazwa;
/**
* #var integer
*
* #ORM\Column(name="idKategoria", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idkategoria;
#THESE NEXT 4 LINES I ADDED
/**
* #ORM\OneToMany(targetEntity="Ksiazka", mappedBy="kategoria")
*/
protected $ksiazki;
/**
* Set nazwa
*
* #param string $nazwa
* #return Kategoria
*/
public function setNazwa($nazwa)
{
$this->nazwa = $nazwa;
return $this;
}
/**
* Get nazwa
*
* #return string
*/
public function getNazwa()
{
return $this->nazwa;
}
/**
* Get idkategoria
*
* #return integer
*/
public function getIdkategoria()
{
return $this->idkategoria;
}
}
This is a fragment I modified in Ksiazka class. I added , inversedBy="ksiazki"
/**
* #var \AppBundle\Entity\Kategoria
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria", inversedBy="ksiazki")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
* })
*/
private $idkategoria;
This is entire Ksiazka class with ManyToOne relation.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Ksiazka
*
* #ORM\Table(name="ksiazka", indexes={#ORM\Index(name="idKategoria_idx", columns={"idKategoria"})})
* #ORM\Entity
*/
class Ksiazka
{
/**
* #var string
*
* #ORM\Column(name="autor", type="string", length=45, nullable=true)
*/
private $autor;
/**
* #var string
*
* #ORM\Column(name="opis", type="text", length=65535, nullable=true)
*/
private $opis;
/**
* #var string
*
* #ORM\Column(name="cena", type="decimal", precision=10, scale=2, nullable=true)
*/
private $cena;
/**
* #var string
*
* #ORM\Column(name="obrazek", type="string", length=45, nullable=true)
*/
private $obrazek;
/**
* #var string
*
* #ORM\Column(name="wydawnictwo", type="string", length=45, nullable=true)
*/
private $wydawnictwo;
/**
* #var string
*
* #ORM\Column(name="rokWydania", type="string", length=45, nullable=true)
*/
private $rokwydania;
/**
* #var string
*
* #ORM\Column(name="isbn", type="string", length=45)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $isbn;
// Było. Automatycznie wygenerowane bez inversedBy.
// /**
// * #var \AppBundle\Entity\Kategoria
// *
// * #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria")
// * #ORM\JoinColumns({
// * #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
// * })
// */
// private $idkategoria;
/**
* #var \AppBundle\Entity\Kategoria
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria", inversedBy="ksiazki")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
* })
*/
private $idkategoria;
/**
* #ORM\OneToMany(targetEntity="Zamowienie_Produkt", mappedBy="ksiazka")
*/
protected $zamowienie_produkty;
/**
* Set autor
*
* #param string $autor
* #return Ksiazka
*/
public function setAutor($autor)
{
$this->autor = $autor;
return $this;
}
/**
* Get autor
*
* #return string
*/
public function getAutor()
{
return $this->autor;
}
/**
* Set opis
*
* #param string $opis
* #return Ksiazka
*/
public function setOpis($opis)
{
$this->opis = $opis;
return $this;
}
/**
* Get opis
*
* #return string
*/
public function getOpis()
{
return $this->opis;
}
/**
* Set cena
*
* #param string $cena
* #return Ksiazka
*/
public function setCena($cena)
{
$this->cena = $cena;
return $this;
}
/**
* Get cena
*
* #return string
*/
public function getCena()
{
return $this->cena;
}
/**
* Set obrazek
*
* #param string $obrazek
* #return Ksiazka
*/
public function setObrazek($obrazek)
{
$this->obrazek = $obrazek;
return $this;
}
/**
* Get obrazek
*
* #return string
*/
public function getObrazek()
{
return $this->obrazek;
}
/**
* Set wydawnictwo
*
* #param string $wydawnictwo
* #return Ksiazka
*/
public function setWydawnictwo($wydawnictwo)
{
$this->wydawnictwo = $wydawnictwo;
return $this;
}
/**
* Get wydawnictwo
*
* #return string
*/
public function getWydawnictwo()
{
return $this->wydawnictwo;
}
/**
* Set rokwydania
*
* #param string $rokwydania
* #return Ksiazka
*/
public function setRokwydania($rokwydania)
{
$this->rokwydania = $rokwydania;
return $this;
}
/**
* Get rokwydania
*
* #return string
*/
public function getRokwydania()
{
return $this->rokwydania;
}
/**
* Get isbn
*
* #return string
*/
public function getIsbn()
{
return $this->isbn;
}
/**
* Set idkategoria
*
* #param \AppBundle\Entity\Kategoria $idkategoria
* #return Ksiazka
*/
public function setIdkategoria(\AppBundle\Entity\Kategoria $idkategoria = null)
{
$this->idkategoria = $idkategoria;
return $this;
}
/**
* Get idkategoria
*
* #return \AppBundle\Entity\Kategoria
*/
public function getIdkategoria()
{
return $this->idkategoria;
}
}
Try to delete all methods in Entity class. Then run doctrine:generate:entities command. If not help, try to find mapping errors in profiler. Hope it help.

Categories