- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个实体:具有多对一关系的 teacher
、subject
(科目很多---一名老师)
,它为我创建了第三个表subject_teacher
我有一个用于创建教师的表单和一个用于创建主题的表单,我可以在其中从选项列表中选择教师。对于每个科目,我想检索每个教师的数据(名字、姓氏、他所教科目的名称)。 1 名教师可以教授 1 门以上的科目。
在我的 Controller 中,我正在做:
$teachers = $em->getRepository('AppBundle:Teacher')->findAll();
$subject = $em->getRepository('AppBundle:Subject')->findByTeachers($teachers);
但我收到错误:
使用参数 [49, 50, 51] 执行“SELECT t0.id AS id_1, t0.name AS name_2 FROM subject t0 WHERE subject_teacher.teacher_id IN (?, ?, ?)”时发生异常:
SQLSTATE[42S22]:未找到列:1054“where 子句”中的未知列“subject_teacher.teacher_id”
神奇的方法findByTeachers
忘记在FROM
子句中添加表subject_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;
}
}
`
最佳答案
我想检索每个科目的所有教师“数据”:
/**** 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%}
关于php - Symfony DQuery 生成器 : Magic method doesn't select the right table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34200852/
这是我要解决的问题,我正在使用 The Fact That Prefix Sum[i] - Prefix Sum[i-1] Leads to Frequency being greater than
我有 2 个实体:具有多对一关系的 teacher、subject (科目很多---一名老师),它为我创建了第三个表subject_teacher 我有一个用于创建教师的表单和一个用于创建主题的表单,
我是一名优秀的程序员,十分优秀!