Kohana 3:has_many 和 order_by

发布于 2024-10-31 10:43:24 字数 41 浏览 5 评论 0原文

如何在 Kohana 3 中订购使用 has_many 关联的查询?

How can i order a query that uses the has_many association in Kohana 3?

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

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

发布评论

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

评论(2

蓝眼泪 2024-11-07 10:43:24

您是否尝试过类似 $model->items->order_by('fieldname')->find_all() 的方法? __get() 方法返回 Query_Builder 对象,而不是 Database_Result,因此您可以根据需要添加 QBuilder 的条件(where/order_by/etc)。

Have you tried something like $model->items->order_by('fieldname')->find_all()? __get() method returns Query_Builder object, not a Database_Result, so you can add QBuilder's conditions (where/order_by/etc) for your needs.

指尖上得阳光 2024-11-07 10:43:24

根据 Kohana_ORM::__get() 实现 - 你不能。

它所做的只是组合 where 条件,而无需添加排序:

    elseif (isset($this->_has_many[$column]))
    {
        $model = ORM::factory($this->_has_many[$column]['model']);

        if (isset($this->_has_many[$column]['through']))
        {
            // Grab has_many "through" relationship table
            $through = $this->_has_many[$column]['through'];

            // Join on through model's target foreign key (far_key) and target model's primary key
            $join_col1 = $through.'.'.$this->_has_many[$column]['far_key'];
            $join_col2 = $model->_table_name.'.'.$model->_primary_key;

            $model->join($through)->on($join_col1, '=', $join_col2);

            // Through table's source foreign key (foreign_key) should be this model's primary key
            $col = $through.'.'.$this->_has_many[$column]['foreign_key'];
            $val = $this->pk();
        }
        else
        {
            // Simple has_many relationship, search where target model's foreign key is this model's primary key
            $col = $model->_table_name.'.'.$this->_has_many[$column]['foreign_key'];
            $val = $this->pk();
        }

        return $model->where($col, '=', $val);
    }

但是您可以编写自己的类 ORM 并在那里重新实现 __get 。您需要重写我上面给出的部分(if isset($this->_has_many[$column]))或将控制传递给 parent::__get( $column) 否则。在这种情况下,您可以随意向 _has_many 设置数组(例如 order_by)添加一个参数,并使用它按相关模型进行排序。

在伪代码中:

class ORM extends Kohana_ORM
{
    public function __get($column)
    {
        $result = parent::__get($column);

        if (isset($this->_has_many[$column]) && !empty($this->_has_many[$column]['order_by'])) {
            $result->order_by($this->_has_many[$column]['order_by']);
        }

        return $result;
    }
}

According to the Kohana_ORM::__get() implementation - you cannot.

All it does is just composing where condition without any possibility of adding a sort:

    elseif (isset($this->_has_many[$column]))
    {
        $model = ORM::factory($this->_has_many[$column]['model']);

        if (isset($this->_has_many[$column]['through']))
        {
            // Grab has_many "through" relationship table
            $through = $this->_has_many[$column]['through'];

            // Join on through model's target foreign key (far_key) and target model's primary key
            $join_col1 = $through.'.'.$this->_has_many[$column]['far_key'];
            $join_col2 = $model->_table_name.'.'.$model->_primary_key;

            $model->join($through)->on($join_col1, '=', $join_col2);

            // Through table's source foreign key (foreign_key) should be this model's primary key
            $col = $through.'.'.$this->_has_many[$column]['foreign_key'];
            $val = $this->pk();
        }
        else
        {
            // Simple has_many relationship, search where target model's foreign key is this model's primary key
            $col = $model->_table_name.'.'.$this->_has_many[$column]['foreign_key'];
            $val = $this->pk();
        }

        return $model->where($col, '=', $val);
    }

But you can write your own class ORM and reimplement __get there. You need to rewrite a little the part I've given above (if isset($this->_has_many[$column])) or pass the control to the parent::__get($column) otherwise. In this case you're free to add one more parameter to _has_many setup array like order_by and use it to order by the related models.

In pseudocode:

class ORM extends Kohana_ORM
{
    public function __get($column)
    {
        $result = parent::__get($column);

        if (isset($this->_has_many[$column]) && !empty($this->_has_many[$column]['order_by'])) {
            $result->order_by($this->_has_many[$column]['order_by']);
        }

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