Kohana 3.2 ORM 定义由两列组成的外键

发布于 2024-12-25 13:16:53 字数 761 浏览 0 评论 0原文

我正在 Kohana 框架上构建一个应用程序。对于内容管理,我将使用 Joomla 框架,因此我们的撰稿人可以在他们熟悉的 UI 中轻松添加和编辑内容。

我定义了一篇文章可以所属的几个类别。例如博客 (catid = 1) 和产品 (catid = 2)。通过 joomla 内容表中的 xreference 列,用户必须将文章分配给所选类别中的特定 ID(因为 Model_Blog 可以包含 id = 1< /code> 以及 Model_Product 都可以)。

因此,Joomla 中的每篇文章都由 catidxreference 的独特组合组成。现在我想将此一对一关系绑定到我的 Kohana ORM 模型(例如 Model_Blog),但标准 Kohana ORM $_has_one 属性仅支持由以下组成的外键一列而不是多个 AFAIK。

我尝试了以下方法,当然,这是行不通的:

protected $_has_one = array(
  'content' => array('model' => 'cms_content', 'foreign_key' => 'xreference', 'catid' => '1')
);

任何人都可以建议我如何正确绑定这种关系吗?

I am building an application on the Kohana framework. For content management, I will be using the Joomla framework, so our copywriters can easily add and edit content in a to them familiar UI.

I have defined several categories to which an article can belong to. E.g. blog (catid = 1) and product (catid = 2). With the xreference column in the joomla content table, the user must assign the article to a specific id in the category selected (because Model_Blog can contain id = 1, as well as Model_Product can).

So every article in Joomla consists of a unique combination of catid and xreference. Now I want to bind this one-to-one relation to my Kohana ORM models (e.g. Model_Blog), but the standard Kohana ORM $_has_one property only supports foreign keys consisting of one column instead of multiple AFAIK.

I tried the following, which, of course, doesn't work:

protected $_has_one = array(
  'content' => array('model' => 'cms_content', 'foreign_key' => 'xreference', 'catid' => '1')
);

Can anyone advise me on how to bind this relationship correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2025-01-01 13:16:53

在查找源代码后,我发现 Kohana ORM 不支持我描述的功能,我必须扩展 ORM 模块(这是因为 Kohana 的性质,小菜一碟:)),以实现多种功能FK 列。

class ORM_Modified extends ORM {
    public function __get($column) {
        if (isset($this->_has_one[$column])) {
            $model = $this->_related($column);
            $pk = $this->pk();

            // Use this model's primary key value (if no value set) and foreign model's column(s)
            if(!is_array($this->_has_one[$column]['foreign_key'])) {
                $col = ;
                $model->where($model->_object_name.'.'.$this->_has_one[$column]['foreign_key'], '=', $pk);
            } else {
                foreach($this->_has_one[$column]['foreign_key'] as $col => $value) {
                    $model->where($model->_object_name.'.'.$col, '=', $value == null ? $pk : $value);
                }
            }
            $model->find();

            return $this->_related[$column] = $model;
        } else {
            return parent::__get($column); 
        }
    }
}

现在,我可以使用以下语法定义一个列数组,这些列构成关系中的外键:

protected $_has_one = array(
    'content' => array('model' => 'cms_content', 'foreign_key' => array('xreference' => null, 'catid' => '1'))
);

请注意,我的解决方案仅适用于 1 对 1 关系,因为在我的情况下,不需要为 1- 实现它to-many,尽管我怀疑这需要类似的修改

After a lookup in the source code, I found out that Kohana ORM doesn't support the feature I described and I had to extend the ORM module (which is because of Kohana's nature, a piece of cake :)), to implement multi-column FK's.

class ORM_Modified extends ORM {
    public function __get($column) {
        if (isset($this->_has_one[$column])) {
            $model = $this->_related($column);
            $pk = $this->pk();

            // Use this model's primary key value (if no value set) and foreign model's column(s)
            if(!is_array($this->_has_one[$column]['foreign_key'])) {
                $col = ;
                $model->where($model->_object_name.'.'.$this->_has_one[$column]['foreign_key'], '=', $pk);
            } else {
                foreach($this->_has_one[$column]['foreign_key'] as $col => $value) {
                    $model->where($model->_object_name.'.'.$col, '=', $value == null ? $pk : $value);
                }
            }
            $model->find();

            return $this->_related[$column] = $model;
        } else {
            return parent::__get($column); 
        }
    }
}

Now I can define an array of columns which form the foreign key in the relationship with the following syntax:

protected $_has_one = array(
    'content' => array('model' => 'cms_content', 'foreign_key' => array('xreference' => null, 'catid' => '1'))
);

Please note that my solution only applies to a 1-to-1 relationship, because in my case there's no need to implement it for 1-to-many, although I suspect this would require a similar modification

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文