【问题标题】:Kohana3 ORM: loading Key/Value AttributesKohana3 ORM:加载键/值属性
【发布时间】:2010-08-17 16:05:40
【问题描述】:

我有两个 MySQL 表(过于简单):

articles
- id
- description

article_attributes
- article_id
- attribute_name
- attribute_value

因此,每个article 可以拥有无​​限数量的attributes。 对于articles,我有一个 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;
?>

有人可以指出正确的方向如何实现这一目标吗?

【问题讨论】:

    标签: php mysql orm kohana-3


    【解决方案1】:

    我认为您需要创建 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();
    

    我不知道你是否解决了这个问题,但我希望这个答案会有所帮助。

    【讨论】:

      【解决方案2】:

      您应该阅读Kohana ORM guide。它处理一对一、一对多(您的情况)和多对多关系。这里的代码我没有复制粘贴,因为有很多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-17
        • 1970-01-01
        • 1970-01-01
        • 2016-04-11
        相关资源
        最近更新 更多