【问题标题】:Modeling a many-to-many with multiple object types in Kohana w/ ORM在 Kohana 中使用 ORM 对具有多种对象类型的多对多建模
【发布时间】:2011-04-05 06:15:23
【问题描述】:

我正在使用 ORM 模块使用 Kohana 3.0.7 构建一个应用程序。我想构建一个对象,例如标签,其中可以标记许多不同种类的对象,并且这些选项可以有多个标签。例如,假设我有 3 个模型:标签、帖子和页面。我将如何构建表格和模型以使其发挥最佳效果?

【问题讨论】:

    标签: php mysql orm kohana


    【解决方案1】:

    您将使用 Kohana 的 has-many-through 关系。一个例子是:

    class Model_Page
    {
        protected $_has_many = array(
            'tags' => array(
                'model' => 'tag',
                'foreign_key' => 'page_id',
                'far_key' => 'tag_id',
                'through' => 'pages_tags',
            ),
        );
    }
    
    class Model_Post
    {
        protected $_has_many = array(
            'tags' => array(
                'model' => 'tag',
                'foreign_key' => 'post_id',
                'far_key' => 'tag_id',
                'through' => 'posts_tags',
            ),
        );
    }
    
    class Model_Tag
    {
        protected $_has_many = array(
            'pages' => array(
                'model' => 'page',
                'foreign_key' => 'tag_id',
                'far_key' => 'page_id',
                'through' => 'pages_tags',
            ),
            'posts' => array(
                'model' => 'post',
                'foreign_key' => 'tag_id',
                'far_key' => 'post_id',
                'through' => 'posts_tags',
            ),
        );
    }
    

    【讨论】:

    • +1 用于提及 Kohana 文档中缺少的 far_key
    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多