1

我有三个使用注释链接在一起的实体。我有一个User,StudentRecording表。

目前,记录实体包含一个学生ID字段,该字段链接到学生实体,学生实体的ID链接到用户实体中的用户ID。

我的问题是,当我通过使用 Doctrine 的查询获得一组 Recording 对象时:

$studentRecordings = $this->getDoctrine()->getRepository('StudentBundle:Recording')
            ->findBy(
            array('pageID' => $pageObject, 'studentID' => $studentObject),
            array('filename' => 'ASC')
        );

我收到无法访问学生信息的错误。

["studentID"]=>
  object(stdClass)#552 (6) {
    ["__CLASS__"]=>
    string(39) "Acme\studentBundle\Entity\Student"
    ["__IS_PROXY__"]=>
    bool(true)
    ["__PROXY_INITIALIZED__"]=>
    bool(false)
    ["id"]=>
    NULL
    ["class"]=>
    NULL
    ["parent"]=>
    NULL
  }

然后这给了我一个错误Call to a member function getId() on a non-object in StudentBundle/Entity/Student.php

录音.php:

class Recording
{

    public $file;

    /**
     * @ORM\Id
     * @ORM\Column(name="id",type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $recordingID;


    /**
     * @ORM\ManyToOne(targetEntity="Page")
     * @ORM\JoinColumn(name="pageID", referencedColumnName="id")
     */
    protected $pageID;

    /**
     * @ORM\ManyToOne(targetEntity="Student")
     * @ORM\JoinColumn(name="student", referencedColumnName="id")
     */
    protected $studentID;

    /**
     * @ORM\Column(name="filename", type="string")
     */
    protected $filename;

学生.php

class Student
{
    /**
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="User")
     * @ORM\JoinColumn(name="id", referencedColumnName="id")
     */
    protected $id;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Classes")
     * @ORM\JoinColumn(name="class", referencedColumnName="id")
     */
    protected $class;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id")
     */
    protected $parent;

用户.php

class User implements UserInterface
{
    /**
     * @ORM\id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $first_name;
    /**
     * @ORM\Column(type="string")
     */
    protected $last_name;
    /**
     * @ORM\ManyToOne(targetEntity="Role")
     * @ORM\JoinColumn(name="role", referencedColumnName="id")
     */
    protected $role;

    /**
     * @ORM\Column(type="string")
     */
    protected $username;
    /**
     * @ORM\Column(type="string")
     */
    protected $password;
    /**
     * @ORM\Column(type="string")
     */
    protected $email;
    /**
     * @ORM\Column(type="string")
     */
    protected $salt;

    /**
     * @ORM\Column(name="is_active", type="integer")
     */
    protected $isActive;

编辑:好的,所以基本上学生对象有一个 getID 函数,它基本上返回$this->student->getID(); 错误是$this->student应该是一个对象,它似乎不在这种情况下。

编辑2:好的,我遇到了一个非常奇怪的情况。假设我有两个 ID 为 1 和 5 的学生帐户,当我将其getRepository()称为学生 1 时,如果我指定'studentID' => 1,它会给出该错误,但是,如果我将其更改为'student' => 5有效。当我登录学生 5 并做同样的事情时,也会发生这种情况。

基本上,如果它与登录的学生匹配,它只会给出错误

4

3 回答 3

1

我认为你不能使用StudentID =1. 你需要有学生对象。StudentID 是您的学生对象。

你命名是错误的。在你的课堂上应该是这样的

$student不保护$studentID

然后在查询中你需要有这样的学生对象

$student = $this->getDoctrine()->getRepository('StudentBundle:Student').find(1)

array('pageID' => $pageObject, 'student' => $student)

于 2013-01-16T07:19:53.920 回答
0

连接列名称中的错字?

class Recording
{
//..

  /**
    * @ORM\ManyToOne(targetEntity="Student")
    * @ORM\JoinColumn(name="student_id", referencedColumnName="id")
    */
    protected $studentID;
}
于 2013-01-16T07:15:52.463 回答
0
/**
  * @ORM\ManyToOne(targetEntity="Student")
  * @ORM\JoinColumn(name="student_id", referencedColumnName="id")
  */
  protected $studentID;

/**
  * @ORM\ManyToOne(targetEntity="Student")
  * @ORM\JoinColumn(name="studentID_id", referencedColumnName="id")
  */
  protected $studentID;

如果您保留 var name 还需要将其保留在 joincolumn

于 2016-04-07T10:43:01.083 回答