I have 2 entity :
teacher
, subject
with a ManyTo One Relation
(subject many--- one teacher)
, which created me a 3rd table subject_teacher
I have a form to create a Teacher, and a form to create a Subject, where i can choose the teacher from an option list. For each subject, I want to retrieve each teachers datas (firstname last name, the name of the subject he's teaching). 1 teacher can teach more than 1 subjects.
In my controller, i'm doing :
$teachers = $em->getRepository('AppBundle:Teacher')->findAll();
$subject = $em->getRepository('AppBundle:Subject')->findByTeachers($teachers);
but i'm getting error :
An exception occurred while executing 'SELECT t0.id AS id_1, t0.name AS name_2 FROM subject t0 WHERE subject_teacher.teacher_id IN (?, ?, ?)' with params [49, 50, 51]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'subject_teacher.teacher_id' in 'where clause'
The magic method findByTeachers
forgot to add the table subject_teacher
in the FROM
clause ?
Or i did something wrong..
Subject entity :
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Subject
*
* @ORM\Table()
* @ORM\Entity
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\SubjectRepository")
*/
class Subject
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Teacher", cascade={"persist"})
*/
private $teachers;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Event", mappedBy="subject", cascade={"persist"})
*/
private $events;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Subject
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set classclassRoom
*
* @param integer $classRoom
*
* @return Subject
*/
public function setClassRoom($classRoom)
{
$this->classRoom = $classRoom;
return $this;
}
/**
* Get classRoom
*
* @return integer
*/
public function getClassRoom()
{
return $this->classRoom;
}
/**
* Constructor
*/
public function __construct()
{
$this->events = new \Doctrine\Common\Collections\ArrayCollection();
$this->teachers = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add event
*
* @param \AppBundle\Entity\Event $event
*
* @return Subject
*/
public function addEvent(\AppBundle\Entity\Event $event)
{
$this->events[] = $event;
return $this;
}
/**
* Remove event
*
* @param \AppBundle\Entity\Event $event
*/
public function removeEvent(\AppBundle\Entity\Event $event)
{
$this->events->removeElement($event);
}
/**
* Get events
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEvents()
{
return $this->events;
}
/**
* Get teachers
*
* @return string
*/
public function getTeachers()
{
return $this->teachers;
}
/**
* Add teacher
*
* @param \AppBundle\Entity\Teacher $teacher
*
* @return Subject
*/
public function addTeacher(\AppBundle\Entity\Teacher $teacher)
{
$this->teachers[] = $teacher;
return $this;
}
/**
* Remove teacher
*
* @param \AppBundle\Entity\Teacher $teacher
*/
public function removeTeacher(\AppBundle\Entity\Teacher $teacher)
{
$this->teachers->removeElement($teacher);
}
}
Teacher entity : `
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Teacher
*
* @ORM\Table()
* @ORM\Entity
*/
class Teacher
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="firstName", type="string", length=255)
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="lastName", type="string", length=255)
*/
private $lastName;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* @param string $firstName
*
* @return Teacher
*/
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 Teacher
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
}
`