Symfony2: ContextErrorException - php

I've been running into a problem when I'm trying to create a list of Movies with specific variable Genre. For example, all Movies with Genre "romance" and so on.
The error is:
ContextErrorException: Notice: Undefined index: genre in /(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
Entity Genre looks like this:
/**
* Genre entity.
*
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Genre.
*
* #package Model
* #ORM\Table(name="genre")
* #ORM\Entity(repositoryClass="AppBundle\Repository\Genre")
*/
class Genre
{
/**
* Id.
*
* #ORM\Id
* #ORM\Column(
* type="integer",
* nullable=false,
* options={
* "unsigned" = true
* }
* )
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #var integer $id
*/
private $id;
/**
* Name.
*
* #ORM\Column(
* name="name",
* type="string",
* length=128,
* nullable=false
* )
*
* #var string $name
*/
private $name;
/**
* Movies array
*
* #ORM\OneToMany(
* targetEntity="AppBundle\Entity\Movie",
* mappedBy="genre"
* )
*/
protected $movies;
/**
* Add movies.
*
* #param \AppBundle\Entity\Movie $movies Movies
*
* #return mixed
*/
public function addMovie(\AppBundle\Entity\Movie $movies)
{
$this->movies[] = $movies;
}
/**
* Remove movies
*
* #param \AppBundle\Entity\Movie $movies Movies
*
* #return mixed
*/
public function removeAnswer(\AppBundle\Entity\Movie $movies)
{
$this->movies->removeElement($movies);
}
/**
* Get movies.
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMovies()
{
return $this->movies;
}
/**
* Constructor
*/
public function __construct()
{
$this->movies = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Remove movies
*
* #param \AppBundle\Entity\Movie $movies Movies
*
* #return mixed
*/
public function removeMovie(\AppBundle\Entity\Movie $movies)
{
$this->movies->removeElement($movies);
}
/**
* Get Id.
*
* #return integer Result
*/
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
/**
* Set name.
*
* #param string $name Name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name.
*
* #return string Name
*/
public function getName()
{
return $this->name;
}
}
While movie looks like this:
/**
* Movie entity.
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Movie.
*
* #package Model
* #ORM\Table(name="movies")
* #ORM\Entity(repositoryClass="AppBundle\Repository\Movie")
*/
class Movie
{
/**
* Id.
*
* #ORM\Id
* #ORM\Column(
* type="integer",
* nullable=false,
* options={
* "unsigned" = true
* }
* )
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #var integer $id
*/
private $id;
/**
* Title.
*
* #ORM\Column(
* name="title",
* type="string",
* length=255,
* nullable=false
* )
*
* #var string $title
*/
private $title;
/**
* Notes.
*
* #ORM\Column(
* name="notes",
* type="text",
* nullable=true
* )
*
* #var string $notes
*/
private $notes;
/**
* Genre array
*
* #ORM\ManyToOne(targetEntity="Genre")
* #ORM\JoinColumn(name="genre_id", referencedColumnName="id")
* )
*
* #var \Doctrine\Common\Collections\ArrayCollection $genres
*/
protected $genres;
/**
* Constructor.
*/
public function __construct()
{
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* #param integer $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Set title.
*
* #param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title.
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set notes.
*
* #param string $notes
*/
public function setNotes($notes)
{
$this->notes = $notes;
}
/**
* Get notes.
*
* #return string
*/
public function getNotes()
{
return $this->notes;
}
/**
* Add genre
*
* #param \AppBundle\Entity\Genre $genres
* #return Movie
*/
public function addGenre(\AppBundle\Entity\Genre $genres)
{
$this->genres[] = $genres;
return $this;
}
/**
* Remove genre
*
* #param \AppBundle\Entity\Genre $genres
*/
public function removeGenre(\AppBundle\Entity\Genre $genres)
{
$this->generes->removeElement($generes);
}
/**
* Get genre
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getGenre()
{
return $this->genres;
}
/**
* Set genre
*
* #param \AppBundle\Entity\Genre $genres
* #return Movie
*/
public function setGenre(\AppBundle\Entity\Genre $genres = null)
{
$this->genres = $genres;
return $this;
}
And while trying to do this, I use this in GenreController:
public function viewAction(Genre $genre)
{
if (!$genre) {
throw new NotFoundHttpException('Genre not found!');
}
$movies= $genre->getMovies();
return $this->templating->renderResponse(
'AppBundle:Genre:view.html.twig',
array('genre' => $genre)
);
}
Now every time I try to generate the page, I get a error message:
ContextErrorException: Notice: Undefined index: genre in /home/(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1758
It's a first time I'm doing something like this, so I'm fully aware it's probably full of errors, but I have no idea where this one is coming from, so it's really hard for me to do anything about it. I would appreciate, if someone would show me my mistakes.

The entity name is Genre, but you reference it as Genere in some parts of the code

Related

How to fix "Could not resolve type of column "id" of class "RelacionesBundle\Entity\Pregunta""?

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")}
* )
*/

Symfony2: how to show valuables from different tables?

I'm trying to create an website in Symfony in which I have a database with Movies. This how it looks like:
/**
* Game entity.
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Movie.
*
* #ORM\Table(name="movies")
* #ORM\Entity(repositoryClass="AppBundle\Repository\Movie")
*/
class Movie
{
/**
* Id.
*
* #ORM\Id
* #ORM\Column(
* type="integer",
* nullable=false,
* options={
* "unsigned" = true
* }
* )
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #var integer $id
*/
private $id;
/**
* Title.
*
* #ORM\Column(
* name="title",
* type="string",
* length=255,
* nullable=false
* )
*
* #var string $title
*/
private $title;
/**
* Notes.
*
* #ORM\Column(
* name="notes",
* type="text",
* nullable=true
* )
*
* #var string $notes
*/
private $notes;
/**
* Genres array
*
* #ORM\ManyToOne(targetEntity="Genre")
* #ORM\JoinColumn(name="genre_id", referencedColumnName="id")
* )
*
* #var \Doctrine\Common\Collections\ArrayCollection $genres
*/
protected $genres;
/**
* Constructor.
*/
public function __construct()
{
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* #param integer $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Set title.
*
* #param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title.
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set notes.
*
* #param string $notes
*/
public function setNotes($notes)
{
$this->notes = $notes;
}
/**
* Get notes.
*
* #return string
*/
public function getNotes()
{
return $this->notes;
}
/**
* Add genres
*
* #param \AppBundle\Entity\Genre $genres
* #return Movie
*/
public function addGenre(\AppBundle\Entity\Genre $genres)
{
$this->genres[] = $genres;
return $this;
}
/**
* Set genres
*
* #param \AppBundle\Entity\Genre $genres
* #return Movie
*/
public function setGenres(\AppBundle\Entity\Genre $genres = null)
{
$this->genres = $genres;
return $this;
}
/**
* Get genres
*
* #return \AppBundle\Entity\Genre
*/
public function getGenres()
{
return $this->genres;
}
}
And each Movie has a Genre:
/**
* Genere entity.
*
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Genere.
*
* #ORM\Table(name="genere")
* #ORM\Entity(repositoryClass="AppBundle\Repository\Genere")
*/
class Genere
{
/**
* Id.
*
* #ORM\Id
* #ORM\Column(
* type="integer",
* nullable=false,
* options={
* "unsigned" = true
* }
* )
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #var integer $id
*/
private $id;
/**
* Name.
*
* #ORM\Column(
* name="name",
* type="string",
* length=128,
* nullable=false
* )
*
* #var string $name
*/
private $name;
/**
* Get Id.
*
* #return integer Result
*/
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
/**
* Set name.
*
* #param string $name Name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name.
*
* #return string Name
*/
public function getName()
{
return $this->name;
}
Now, my question is: how can I show a genre in movies list? I know how to make a list of movies, but no idea what to use to show genres.
My MovieController currently looks like this:
/**
* List action.
*
* #Route("/movie-list", name="movies-list")
* #Route("/movies-list/")
*
* #throws NotFoundHttpException
* #return Response A Response instance
*/
public function listAction()
{
$movies = $this->findByStatId(2);
if (!$movies) {
throw new NotFoundHttpException('Movies not found!');
}
return $this->templating->renderResponse(
'AppBundle:Movie:index.html.twig',
array('movies' => $movies)
);
}
What should I add to be able to show genres? Where? Any advises would be welcome.
You already have genres along with $movies variable. You just have to call access genres like this:
{% for genre in movies.genres %}
{# Access each genre variable here #}
{% endfor %}
movies is a object passed to twig file and it has property genres to access related genres in it.
To access genres on controller:
<?php
$genres=$movies->getGenres();
// loop through $genres.
?>
getGenres() will return collection of genres.
You have a mistake in your Genre class. At least in your example, you call it Genere instead of Genre. Second, if a movie should have more than one genre then you should make a many-to-many association, as multiple movies can have the same genre. If a movie can only have one genre, then you have to switch the owning side with Genre (Movie should have Genre (singular), Genre should have Movies (plural))

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

Mapping one-to-one relationship + Symfony2

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

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