本文介绍了在Kohana w/ORM中使用多种对象类型对多对多建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on building an app with Kohana 3.0.7, using the ORM module. I want to build an object, such as tag, where many different kinds of objects can be tagged, and these options can have multiple tags. So let's say for example I have 3 models: Tag, Post and Page. how would I structure the tables and the models to make this work best?

推荐答案

您将使用Kohana的具有多通关系.一个例子是:

You'd use Kohana's has-many-through relationship. An example would be:

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',
        ),
    );
}

这篇关于在Kohana w/ORM中使用多种对象类型对多对多建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 10:34