php - Kohana3 ORM : loading Key/Value Attributes

标签 php mysql orm kohana-3

我有两个 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/

相关文章:

php - 无连续字母相同的排列数

php - Symfony 和 Twig 模板。如何动态扩展模板或什么也不扩展?

php - Mysql 查询与 php 查询给我错误的结果

php - 如何使用sql中的两个表数据进行管理登录?

mysql - Node.js + MySQL : Time is 2 hours off

php - 注册表类的目的是什么?

PHP:带引号的评论回复系统。如何向子(回复)消息显示父(引用)消息

python - Django,序列化关系,对象而不是id

java - Spring 冷冻应用

c# - SubSonic 3 简单存储库问题