Kohana-v3 ORM 父关系
我刚刚开始使用 Kohana 框架的版本 3。 我对 $_has_many 等做了一些工作。
现在我有了表格页面。主键是pageID。该表有一列称为parentPageID。现在我想要创建一个 ORM 模型,当像这样访问时 $page->parent->find()
返回由parentPageID 标识的页面。
我已经有以下内容:
// Settings
protected $_table_name = 'pages';
protected $_primary_key = 'pageID';
protected $_has_one = array(
'parent' => array(
'model' => 'page',
'foreign_key' => 'parentPageID',
),
);
但这不起作用,它只是返回表中的第一页。最后一个查询是这样的:
SELECT `pages`.* FROM `pages` ORDER BY `pages`.`pageID` ASC LIMIT 1
有人知道如何解决这个问题吗?
我知道这可以: $parent = $page->parent->find($page->parentPageID);
但它必须而且可以更干净(我认为)。
已解决,请参阅下面我的回答。
I just started with the version 3 of the Kohana Framework.
I have worked a little with the $_has_many etc.
Now I have the table pages. The primary key is pageID. The table has a column called parentPageID. Now I want to make a ORM model who, when accesed like this $page->parent->find()
returns the page identified by parentPageID.
I have the following already:
// Settings
protected $_table_name = 'pages';
protected $_primary_key = 'pageID';
protected $_has_one = array(
'parent' => array(
'model' => 'page',
'foreign_key' => 'parentPageID',
),
);
But that does not work, it simply returns the first page from the table. Last query says this:
SELECT `pages`.* FROM `pages` ORDER BY `pages`.`pageID` ASC LIMIT 1
Does somebody know how to solve this?
I know this can: $parent = $page->parent->find($page->parentPageID);
but it must be and can be cleaner (I think).
Solved, see my answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己解决了。我需要交换这些东西。我解释一下:
你可以造这样一句话:一个页面属于他的父页面。
所以,当我这样想的时候,我知道我做错了什么。
不,我有这个(完美的):
我可以这样使用它:
@Stackoverflow
抱歉,我用我自己回答的问题填充了您的数据库。
I solved it my self. I needed to swap the things. I explain:
You can make a sentence like this: A page belongs to his parent page.
So, when I thought like that I know what i did wrong.
No I have this (what works perfectly):
I can use it like this:
@Stackoverflow
Sorry I filled your database with a question I answered my self.