Mapping one-to-one relationship + Symfony2 - php

I have a database with a table users with fields: user_id, username, password, ... . I also have a table players with player_name, player_position, ... AND a FK user_id.
Image relationship:
I genered the entities from the database but now I would like to get the player from user like this $user->getPlayer(). And doctrine didn't generate player in Users entity, only user in Players Entity.
This is what I have now:
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
* #JoinColumn(name="user_id", referencedColumnName="userId")
*/
private $player;
/**
* Get player
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Players
*/
public function getPlayer() {
return $this->player;
}
/**
* Set player
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Players $player
* #return Users
*/
public function setPlayer(\VolleyScout\VolleyScoutBundle\Entity\Players $player = null){
$this->player = $player;
return $this;
}
But when I try $user->getPlayer() I always get null. (And the user_id is in players table)
UPDATE:
My Players Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Players
*
* #ORM\Table(name="players", indexes={#ORM\Index(name="fk_players_users1_idx", columns={"user_id"}), #ORM\Index(name="fk_players_teams1_idx", columns={"team_id"}), #ORM\Index(name="fk_players_myteam1_idx", columns={"myteam_id"})})
* #ORM\Entity
*/
class Players
{
/**
* #var string
*
* #ORM\Column(name="player_name", type="string", length=255, nullable=false)
*/
private $playerName;
/**
* #var string
*
* #ORM\Column(name="player_licensenumber", type="string", length=45, nullable=false)
*/
private $playerLicensenumber;
/**
* #var string
*
* #ORM\Column(name="player_position", type="string", nullable=false)
*/
private $playerPosition;
/**
* #var integer
*
* #ORM\Column(name="player_birthyear", type="integer", nullable=true)
*/
private $playerBirthyear;
/**
* #var integer
*
* #ORM\Column(name="player_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $playerId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Myteam
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Myteam")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="myteam_id", referencedColumnName="myteam_id")
* })
*/
private $myteam;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Teams
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
* })
*/
private $team;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Users
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Set playerName
*
* #param string $playerName
* #return Players
*/
public function setPlayerName($playerName)
{
$this->playerName = $playerName;
return $this;
}
/**
* Get playerName
*
* #return string
*/
public function getPlayerName()
{
return $this->playerName;
}
/**
* Set playerLicensenumber
*
* #param string $playerLicensenumber
* #return Players
*/
public function setPlayerLicensenumber($playerLicensenumber)
{
$this->playerLicensenumber = $playerLicensenumber;
return $this;
}
/**
* Get playerLicensenumber
*
* #return string
*/
public function getPlayerLicensenumber()
{
return $this->playerLicensenumber;
}
/**
* Set playerPosition
*
* #param string $playerPosition
* #return Players
*/
public function setPlayerPosition($playerPosition)
{
$this->playerPosition = $playerPosition;
return $this;
}
/**
* Get playerPosition
*
* #return string
*/
public function getPlayerPosition()
{
return $this->playerPosition;
}
/**
* Set playerBirthyear
*
* #param integer $playerBirthyear
* #return Players
*/
public function setPlayerBirthyear($playerBirthyear)
{
$this->playerBirthyear = $playerBirthyear;
return $this;
}
/**
* Get playerBirthyear
*
* #return integer
*/
public function getPlayerBirthyear()
{
return $this->playerBirthyear;
}
/**
* Get playerId
*
* #return integer
*/
public function getPlayerId()
{
return $this->playerId;
}
/**
* Set myteam
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam
* #return Players
*/
public function setMyteam(\VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam = null)
{
$this->myteam = $myteam;
return $this;
}
/**
* Get myteam
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Myteam
*/
public function getMyteam()
{
return $this->myteam;
}
/**
* Set team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
* #return Players
*/
public function setTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team = null)
{
$this->team = $team;
return $this;
}
/**
* Get team
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Teams
*/
public function getTeam()
{
return $this->team;
}
/**
* Set user
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Users $user
* #return Players
*/
public function setUser(\VolleyScout\VolleyScoutBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
UPDATE 2: In my Users Entity Class I have now:
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $player;
But it still doesn't work ..

Your annotations are not set correctly (specifically, the JoinColumn). It is interesting that you aren't getting an error, but anyways... replace this:
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
* #JoinColumn(name="user_id", referencedColumnName="userId")
*/
private $player;
with this:
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
*/
private $player;
notice that your referencedColumnName should be the name as it is shown in the database, not the Doxtrine entity. Start by fixing those errors, and it should solve the problem.
UPDATE
This is a very different setup it you want a manyToOne relationship.
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Players
*
* #ORM\OneToMany(targetEntity="Players", mappedBy="user_id")
*/
private $players;
public function __construct()
{
$this->players = new ArrayCollection();
}
Because $players can now have many values, it should be set as an ArrayCollection. I also made the variable $players instead of $player, for ease of readability.
You should really familiarize yourself with the Symfony Docs, this is a very simple problem that is described very clear in the Symfony docs

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!

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

doctrine one-to-one relation mapping

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;
}

Where query + Doctrine

I have table item with item_id, item_title, item_description, item_created, item_approved. I also have a table article with PK item_id (from item table) and article_body.
Now I would like to select all the articles where item.item_approved is NOT equal to NULL. But I'm stuck with creating the query. This is what I have now:
$entityManager = $this->getDoctrine()->getManager();
$repository = $entityManager->getRepository('VolleyScoutBundle:Article');
$query = $repository->createQueryBuilder('a')
->where('a.item.ItemApproved != NULL')
->getQuery();
$articles = $query->getResult();
This gave me the error: [Syntax Error] line 0, col 73: Error: Expected =, <, <=, <>, >, >=, !=, got '.'
This is my Article Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Article
*
* #ORM\Table(name="article")
* #ORM\Entity
*/
class Article
{
/**
* #var string
*
* #ORM\Column(name="article_body", type="text", nullable=false)
*/
private $articleBody;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Item
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Item")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="item_id", referencedColumnName="item_id")
* })
*/
private $item;
/**
* Set articleBody
*
* #param string $articleBody
* #return Article
*/
public function setArticleBody($articleBody)
{
$this->articleBody = $articleBody;
return $this;
}
/**
* Get articleBody
*
* #return string
*/
public function getArticleBody()
{
return $this->articleBody;
}
/**
* Set item
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Item $item
* #return Article
*/
public function setItem(\VolleyScout\VolleyScoutBundle\Entity\Item $item)
{
$this->item = $item;
return $this;
}
/**
* Get item
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Item
*/
public function getItem()
{
return $this->item;
}
}
This is my Item Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Item
*
* #ORM\Table(name="item", indexes={#ORM\Index(name="fk_item_users1_idx", columns={"user_id"}), #ORM\Index(name="fk_item_myteam1_idx", columns={"myteam_id"})})
* #ORM\Entity
*/
class Item
{
/**
* #var string
*
* #ORM\Column(name="item_title", type="string", length=255, nullable=false)
*/
private $itemTitle;
/**
* #var string
*
* #ORM\Column(name="item_description", type="text", nullable=false)
*/
private $itemDescription;
/**
* #var \DateTime
*
* #ORM\Column(name="item_created", type="datetime", nullable=false)
*/
private $itemCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="item_approved", type="datetime", nullable=true)
*/
private $itemApproved;
/**
* #var \DateTime
*
* #ORM\Column(name="item_deleted", type="datetime", nullable=true)
*/
private $itemDeleted;
/**
* #var integer
*
* #ORM\Column(name="item_id", type="bigint")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $itemId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Myteam
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Myteam")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="myteam_id", referencedColumnName="myteam_id")
* })
*/
private $myteam;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Users
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Set itemTitle
*
* #param string $itemTitle
* #return Item
*/
public function setItemTitle($itemTitle)
{
$this->itemTitle = $itemTitle;
return $this;
}
/**
* Get itemTitle
*
* #return string
*/
public function getItemTitle()
{
return $this->itemTitle;
}
/**
* Set itemDescription
*
* #param string $itemDescription
* #return Item
*/
public function setItemDescription($itemDescription)
{
$this->itemDescription = $itemDescription;
return $this;
}
/**
* Get itemDescription
*
* #return string
*/
public function getItemDescription()
{
return $this->itemDescription;
}
/**
* Set itemCreated
*
* #param \DateTime $itemCreated
* #return Item
*/
public function setItemCreated($itemCreated)
{
$this->itemCreated = $itemCreated;
return $this;
}
/**
* Get itemCreated
*
* #return \DateTime
*/
public function getItemCreated()
{
return $this->itemCreated;
}
/**
* Set itemApproved
*
* #param \DateTime $itemApproved
* #return Item
*/
public function setItemApproved($itemApproved)
{
$this->itemApproved = $itemApproved;
return $this;
}
/**
* Get itemApproved
*
* #return \DateTime
*/
public function getItemApproved()
{
return $this->itemApproved;
}
/**
* Set itemDeleted
*
* #param \DateTime $itemDeleted
* #return Item
*/
public function setItemDeleted($itemDeleted)
{
$this->itemDeleted = $itemDeleted;
return $this;
}
/**
* Get itemDeleted
*
* #return \DateTime
*/
public function getItemDeleted()
{
return $this->itemDeleted;
}
/**
* Get itemId
*
* #return integer
*/
public function getItemId()
{
return $this->itemId;
}
/**
* Set myteam
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam
* #return Item
*/
public function setMyteam(\VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam = null)
{
$this->myteam = $myteam;
return $this;
}
/**
* Get myteam
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Myteam
*/
public function getMyteam()
{
return $this->myteam;
}
/**
* Set user
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Users $user
* #return Item
*/
public function setUser(\VolleyScout\VolleyScoutBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
try:
$query = $repository->createQueryBuilder('a')
->join('a.item', 'i')
->where('i.ItemApproved is not NULL')
->getQuery();

Doctrine only selects only first row data, no matter what

I have the problem when I want to select for example all the categories in my table that Doctrine only selects the first row data.
For example:
I have 3 categories (id: 1, 2 & 3). I want to select all the
categories by doing this: $categories =
$entityManager->getRepository('ReuzzeReuzzeBundle:Categories')
->findAll();
What I get now is: 3 times the first entity!
I have 3 categories (id: 1, 2 & 3). I want to select the category
with id = 2 by doing this: $category =
$entityManager->getRepository('ReuzzeReuzzeBundle:Categories')->findOneByCategoryId(2);
then I get again the first category with id = 1 ...
Is this is a settings problem or other ... ? Can somebody help me with this? It's very annoying when you can't select all the data :/
UPDATE:
My Entity Categories Class:
<?php
namespace Reuzze\ReuzzeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Categories
*
* #ORM\Table(name="categories", indexes={#ORM\Index(name="fk_categories_categories1_idx", columns={"category_parentid"})})
* #ORM\Entity
*/
class Categories
{
/**
* #var boolean
*
* #ORM\Column(name="category_id", type="boolean")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
/**
* #var string
*
* #ORM\Column(name="category_name", type="string", length=45, nullable=false)
*/
private $categoryName;
/**
* #var string
*
* #ORM\Column(name="category_description", type="string", length=255, nullable=false)
*/
private $categoryDescription;
/**
* #var \DateTime
*
* #ORM\Column(name="category_created", type="datetime", nullable=false)
*/
private $categoryCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="category_modified", type="datetime", nullable=true)
*/
private $categoryModified;
/**
* #var \DateTime
*
* #ORM\Column(name="category_deleted", type="datetime", nullable=true)
*/
private $categoryDeleted;
/**
* #var \Reuzze\ReuzzeBundle\Entity\Categories
*
* #ORM\ManyToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Categories")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_parentid", referencedColumnName="category_id")
* })
*/
private $categoryParentid;
/**
* Get categoryId
*
* #return boolean
*/
public function getCategoryId()
{
return $this->categoryId;
}
/**
* Set categoryName
*
* #param string $categoryName
* #return Categories
*/
public function setCategoryName($categoryName)
{
$this->categoryName = $categoryName;
return $this;
}
/**
* Get categoryName
*
* #return string
*/
public function getCategoryName()
{
return $this->categoryName;
}
/**
* Set categoryDescription
*
* #param string $categoryDescription
* #return Categories
*/
public function setCategoryDescription($categoryDescription)
{
$this->categoryDescription = $categoryDescription;
return $this;
}
/**
* Get categoryDescription
*
* #return string
*/
public function getCategoryDescription()
{
return $this->categoryDescription;
}
/**
* Set categoryCreated
*
* #param \DateTime $categoryCreated
* #return Categories
*/
public function setCategoryCreated($categoryCreated)
{
$this->categoryCreated = $categoryCreated;
return $this;
}
/**
* Get categoryCreated
*
* #return \DateTime
*/
public function getCategoryCreated()
{
return $this->categoryCreated;
}
/**
* Set categoryModified
*
* #param \DateTime $categoryModified
* #return Categories
*/
public function setCategoryModified($categoryModified)
{
$this->categoryModified = $categoryModified;
return $this;
}
/**
* Get categoryModified
*
* #return \DateTime
*/
public function getCategoryModified()
{
return $this->categoryModified;
}
/**
* Set categoryDeleted
*
* #param \DateTime $categoryDeleted
* #return Categories
*/
public function setCategoryDeleted($categoryDeleted)
{
$this->categoryDeleted = $categoryDeleted;
return $this;
}
/**
* Get categoryDeleted
*
* #return \DateTime
*/
public function getCategoryDeleted()
{
return $this->categoryDeleted;
}
/**
* Set categoryParentid
*
* #param \Reuzze\ReuzzeBundle\Entity\Categories $categoryParentid
* #return Categories
*/
public function setCategoryParentid(\Reuzze\ReuzzeBundle\Entity\Categories $categoryParentid = null)
{
$this->categoryParentid = $categoryParentid;
return $this;
}
/**
* Get categoryParentid
*
* #return \Reuzze\ReuzzeBundle\Entity\Categories
*/
public function getCategoryParentid()
{
return $this->categoryParentid;
}
}
It looks like your problem is with you annotations definition of your categoryId. You have defined it as a boolean. This should be an integer. That would probably be why you are only getting the category with id 1.

Categories