如何在原则 2 中使用多对一关联
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Schools 实体中,您希望有这样的东西有了这个
,您可以执行类似以下的操作,这会给您一个 ArrayCollection 包含所有学生对象
希望有所帮助...Docrine 2 非常强大,但有些东西没有很好地记录。有关此内容的更多信息,请参阅使用关联。
In the Schools entity you'd want to have something like this
With this you could do something like the following, which gives you an ArrayCollection with all student objects
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.
对于那些面临这个问题的人:
“致命错误:在第 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")
我已经取得了一些进步。我读了“使用对象' 部分。
关联的对象可以通过这样的方式访问(根据我的问题中给出的示例):
但是在我原来的问题中,我想通过学校对象访问属于学校的学生。我完全不知道如何做到这一点,在我看来这是不可能的。
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):
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.