【问题标题】:Kohana 3 simple relationsKohana 3 简单的关系
【发布时间】:2010-05-22 08:19:57
【问题描述】:

我正在尝试在 kohana 3 Web 框架中编写一个非常简单的 cms(用于学习目的)。 我有我的数据库架构,我想将它映射到 ORM,但我遇到了关系问题。

架构:articlescategories

一篇文章有​​一个类别。当然,一个类别可能有很多文章。

我认为它是文章表中的 has_one 关系。(?)

现在是 php 代码。我需要先创建 application/classes/models/article.php,是吗?

class Model_Article extends ORM
{
    protected // and i am not sure what i suppose to write here       
}

【问题讨论】:

    标签: php kohana kohana-orm kohana-3 sql


    【解决方案1】:
    class Model_Article extends ORM{
    
     protected $_belongs_to = array
     (
      'category'  => array(), // This automatically sets foreign_key to category_id and model to Model_Category (Model_$alias)
     );
    
    }
    
    class Model_Category extends ORM{
    
     protected $_has_many = array
     (
      'articles' => array(), // This automatically sets foreign_key to be category_id and model to Model_Article (Model_$alias_singular)
     );
    
    }
    

    你也可以手动定义关系;

    'articles' => array('model'=>'article','foreign_key'=>'category_id');
    

    More about Kohana 3 ORM

    More about Kohana ORM naming conventions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多