代码之家  ›  专栏  ›  技术社区  ›  GSto

在Kohana w/ORM中用多种对象类型对多对多进行建模

  •  1
  • GSto  · 技术社区  · 16 年前

    我正在使用ORM模块与Kohana3.0.7开发一个应用程序。我想构建一个对象,例如标记,在那里可以标记许多不同类型的对象,并且这些选项可以有多个标记。例如,我有3个模型:标签、文章和页面。我将如何构造表和模型以使其发挥最佳作用?

    1 回复  |  直到 16 年前
        1
  •  3
  •   Zahymaka    16 年前

    你可以用科哈纳的 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',
            ),
        );
    }
    
    推荐文章