Symfony DQuery 生成器:魔术方法 select 不正确 table

Symfony DQuery builder: Magic method doesn't select the right table

我有 2 个实体: teachersubject 具有多对一关系

(subject many--- one teacher),这让我获得了第 3 个 table subject_teacher

我有一个创建教师的表单和一个创建主题的表单,我可以从选项列表中选择教师。 对于每个科目,我想检索每个教师的数据(名字姓氏,他所教科目的名称)。 1位老师可以教多于1个科目。

在我的控制器中,我正在做:

$teachers = $em->getRepository('AppBundle:Teacher')->findAll(); $subject = $em->getRepository('AppBundle:Subject')->findByTeachers($teachers);

但我收到错误消息:

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'

魔术findByTeachers忘记在FROM子句中添加tablesubject_teacher? 或者我做错了什么..

主题实体:

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

}

教师实体: `

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

我想检索每个科目的所有老师"data":

/**** CONTROLLER ****/

$subjects = $em->getRepository('AppBundle:Subject')->findAll();


//If you need to threat results inside the controller:

$results = [];

foreach ($subjects as $subject) {
     $results[]['name'] = $subject->getName();
     $results[]['teacher_fistname'] = $subject->getTeachers()->getFirstName();
     $results[]['teacher_lastname'] = $subject->getTeachers()->getLastName();
    /.../
}

return $results;





// If you only want to display results in the view:

return $this->render('your-template.html.twig', ['subjects' => $subjects ]);

/**** And in the view ****/

{% for subject in subjects %}
  Subject: {{ subject.name }}
  Teacher Fistname: {{ subject.teachers.firstname }}
  Teacher Lastname: {{ subject.teachers.lastname }}
  /.../

{% endfor%}