AR模型中的关系函数,多对一关系
所以情况如下:
我有两个表,Issue 和 Issue 。项目。
一个项目可以有多个问题,一个问题只能是一个项目。
既然Issue是多对一的,那么你必须定义它吗?
因为我知道在项目模型中我有:
public function relations()
{
return array(
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
);
}
对于问题模型,我除了外键什么都没有:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
);
}
我猜任何一个关系都不需要定义?
先感谢您。
顺便说一句,我正在写《敏捷 Yii》一书,最后我问了自己这个问题。 AR 类中有一个 has-one 选项(http://www.yiiframework.com/ doc/guide/database.arr)。
但由于某种原因,这种情况是可选的吗?
So here's the scenario:
I have two table, Issue & Project.
A Project can have many Issue and an Issue can exactly one project.
Since Issue is many to one, do you have to define it?
Cause I know in Project Model I have:
public function relations()
{
return array(
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
);
}
For Issue Model I have nothing but foreign keys:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
);
}
I'm guessing anything to one relationship does not need to be define?
Thank you in advance.
BTW, I'm doing agile Yii book and I ended up asking myself this question.
There's a has-one option in AR class (http://www.yiiframework.com/doc/guide/database.arr).
But is this case optional for some reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它帮助我将 BELONGS_TO 和 HAS_ONE 之间的区别视为“外键存储在哪里”?如果项目模型存储“Issue_Id”,则潜在问题可能有许多项目。您使用 HAS_ONE 关系来声明即使问题可以有许多项目,它也只有一个。
但是,更常见的情况是您将 Project_Id 存储在问题模型中(我假设您是这样)。那么您必须使用 BELONGS_TO 关系。 看来您已经在上面正确定义了关系。
有人在这里发布了一个与 Yii 关系相关的类似问题,我帮助回答:
yii - 使用关系 HAS_ONE 从相关表中获取数据以显示在列表页面中
至于您对“需要”定义关系的担忧,您不需要“需要”定义任何关系。您可以编写自己的 SQL 查询来执行相同的操作。 ActiveRecord 关系只是一个方便的东西,可以使相关记录的查询变得更简单。如果您永远不会查找问题的项目,那么您“不需要”定义“项目”BELONGS_TO 关系。
在没有实际看到您的数据库结构的情况下,在我看来您的所有设置都正确。现在,您可以轻松地创建 $issue->project 和 $project->issues “惰性”关系查询,充分利用关系活动记录的强大功能。干杯并祝项目顺利!
It helps me to think of the difference between BELONGS_TO and HAS_ONE as "where is the foreign key stored"? If the Project model stored "Issue_Id" then potentially Issue could have many Projects. You use the HAS_ONE relationship to state that even if Issue COULD have many projects, it only has ONE.
However, the more common case is if you are storing the Project_Id in the Issue model (and I assume you are). Then you have to use the BELONGS_TO relationship. It appears you have defined the relationships correctly above.
Someone posted a similar question relating to Yii relations here that I helped answer:
yii - using relation HAS_ONE to get data from the related table to display in list page
As to your concern about "needing" to define relationships, you don't "need" to define any. You could write your own SQL queries to do the same thing. The ActiveRecord relations are just a convenience thing to make querying for related records simpler. If you are never going to look up an Issue's Project, then you don't "need" to define the "project" BELONGS_TO relationship.
Without actually seeing your database structure it looks to me like you have everything set up correctly. You can now easily make both $issue->project and $project->issues "lazy" relational queries, taking full advantage of the power of the Relational Active Record. Cheers and good luck with project!
在您的问题模型中,您已经将关系指定为 BELONGS_TO 项目模型。
我也刚刚拿到了新的 yii-book。它对我帮助很大!
快乐编码:)
In your Issue model you already have the relation specified as BELONGS_TO the project model.
I also just got the new yii-book. It helped me alot!
Happy coding :)