如何在原则 2 中使用多对一关联

发布于 2024-11-25 09:30:19 字数 679 浏览 4 评论 0原文

我在 Zend 中使用 Doctrine 2。

我试图弄清楚如何从当前对象访问相关模型的属性/方法。

例如,我们有两个表:学校和学生。

许多学生都属于一所学校,因此这是一种多对一关系,我只对列出每所学校的所有学生感兴趣。我不想查询学生记录来查找他们所属学校的详细信息,因此这被归类为单向关系。

现在,要在原则 2 中的表之间设置多对一关系,我将在 Students 实体中添加此关系,因为它是拥有方

/**
 * @ManyToOne(targetEntity="Schools")
 * @JoinColumn(name="school_id", referencedColumnName="school_id")
 */
private $schoolId;

其中 name 值分别对应students表和schools表中的列名。

因此,如果我有学校记录的对象,如何访问学生属性/方法?

echo $oSchool->Students->getName(); // doesn't work

我不明白我做错了什么,正在生成代理类。如果有人能指出我正确的方向,我将不胜感激。

I'm using Doctrine 2 in Zend.

I'm trying to figure out how to access the properties/method for related models from the current object.

For example, we have two tables: Schools and Students.

Many Students belong to a school, so this is a many to one relationship and I'm only interested in listing all the students for each school. I do not wish to query student records to find the details of the school they belong to therefore this is classed as a unidirectional relationship.

Now to set up the many to one relationship in Doctrine 2 between the tables I'd add this in the Students entity, as it is the owning side:

/**
 * @ManyToOne(targetEntity="Schools")
 * @JoinColumn(name="school_id", referencedColumnName="school_id")
 */
private $schoolId;

Where the name values correspond the column names in the students table and schools table respectively.

So if I have an object of a Schools record, how do I access the student properties/methods?

echo $oSchool->Students->getName(); // doesn't work

I can't understand what I have done wrong, the proxy class is being generated. Appreciate it if anyone could point me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阪姬 2024-12-02 09:30:19

在 Schools 实体中,您希望有这样的东西有了这个

/**
 * @param \Doctrine\Common\Collections\Collection $property
 * @OneToMany(targetEntity="Students", mappedBy="school")
 */
private $student;

public function __construct() {
    $this->student = new \Doctrine\Common\Collections\ArrayCollection();
}

public function getStudent() {
    return $this->student;
}

,您可以执行类似以下的操作,这会给您一个 ArrayCollection 包含所有学生对象

$studentsArray = $school->getStudent();

希望有所帮助...Docrine 2 非常强大,但有些东西没有很好地记录。有关此内容的更多信息,请参阅使用关联。

In the Schools entity you'd want to have something like this

/**
 * @param \Doctrine\Common\Collections\Collection $property
 * @OneToMany(targetEntity="Students", mappedBy="school")
 */
private $student;

public function __construct() {
    $this->student = new \Doctrine\Common\Collections\ArrayCollection();
}

public function getStudent() {
    return $this->student;
}

With this you could do something like the following, which gives you an ArrayCollection with all student objects

$studentsArray = $school->getStudent();

Hope that helps... Docrine 2 is very powerful but some things are not documented very well. Some more information on this in the Documentation at Working with Assiciations.

他夏了夏天 2024-12-02 09:30:19

对于那些面临这个问题的人:
“致命错误:在第 168 行对 C:\xampplite\htdocs\test\library\Doctrine\ORM\PersistentCollection.php 中的非对象调用成员函数 setValue() –”

解决方案:需要用学说属性值而不是表列名称替换mappedBy值

例如:

正确语法: @ORM\OneToMany(targetEntity="Test\Entity\School", mappedBy="school")

错误语法:正确语法:@ORM\OneToMany(targetEntity="Test\Entity\School",mappedBy="school_id")

For those who are facing this issue:
"Fatal error: Call to a member function setValue() on a non-object in C:\xampplite\htdocs\test\library\Doctrine\ORM\PersistentCollection.php on line 168 –"

Solution: Need to replace mappedBy value with doctrine property value instead of Table column name

Ex:

Correct Syntax: @ORM\OneToMany(targetEntity="Test\Entity\School", mappedBy="school")

Wrong Syntax: Correct syntax: @ORM\OneToMany(targetEntity="Test\Entity\School", mappedBy="school_id")

雅心素梦 2024-12-02 09:30:19

我已经取得了一些进步。我读了“使用对象' 部分。

关联的对象可以通过这样的方式访问(根据我的问题中给出的示例):

$oStudent->getSchoolId()->getName(); // gets a student's school's name by traversing the school class

但是在我原来的问题中,我想通过学校对象访问属于学校的学生。我完全不知道如何做到这一点,在我看来这是不可能的。

I've made some progress. I read the 'Working with Objects' section of Doctrine 2's documentation.

Associated objects can be accessed by something like this (according to the example given in my question):

$oStudent->getSchoolId()->getName(); // gets a student's school's name by traversing the school class

However in my orignal question, I wanted to access the students belonging to a school via the schools object. I am completely stumped on how to do this, It seems to me its not possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文