我有两个 MySQL 表(过于简单):
articles
- id
- description
article_attributes
- article_id
- attribute_name
- attribute_value
因此,每个文章
都可以有无限数量的属性
。
对于文章
,我有一个 Kohana_ORM 模型
<?php class Model_Article extends ORM {} ?>
加载模型时,我想访问所有属性。
例子:
<?php
$article = new Model_Article(1); // or ORM::factory('article', 1);
echo $article->description;
echo $article->attribute->foo;
echo $article->attribute->bar;
?>
有人可以指出正确的方向如何实现这一目标吗?
最佳答案
我认为您需要创建 2 个模型,每个表一个,然后定义它们之间的关系。
试试这个,
class Model_Article extends ORM
{
protected $_has_many = array('attributes' => array());
}
class Model_Atribute extends ORM
{
protected $_table_name = 'article_attributes';
protected $_belongs_to = array('article' => array());
}
那么你可以这样做,
$article = ORM::factory('article', 1);
$article_attributes = $article->attributes->find_all();
我不知道你是否解决了这个问题,但我希望这个答案对你有所帮助。
关于php - Kohana3 ORM : loading Key/Value Attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3504525/