douyuan6490 2015-12-10 11:34
浏览 36
已采纳

Symfony DQuery构建器:Magic方法没有选择正确的表

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;
}
}
`
  • 写回答

1条回答 默认 最新

  • doubi5520 2015-12-10 14:25
    关注

    I you want to retrieve all the Teacher "data" for each subject:

    /**** 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%}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!