如何订购在Kohana 3中使用has_many关联的查询?Kohana 3:has_many和order_by
0
A
回答
3
你试过类似$model->items->order_by('fieldname')->find_all()
? __get()
方法返回Query_Builder对象,而不是Database_Result,因此您可以根据需要添加QBuilder的条件(where/order_by/etc)。
1
根据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
那里。您需要重写一下上面给出的部分(如果是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;
}
}
相关问题
- 1. Kohana ORM has_many belongs_to
- 2. kohana - 帮助has_many
- 3. Kohana has_many relationship
- 4. Adodb和Kohana 3
- 5. Kohana的3 ORM - 连接和静态方法的has_many关系
- 6. Kohana ORM关系$ has_many和$ belongs_to和外键
- 7. Kohana - has_many和belongs_to不能正常工作
- 8. Kohana ORM - mysql内部order_by()方法
- 9. Kohana 3.1 ORM:加入has_many
- 10. Codeigniter 2和Kohana 3教程?
- 11. Kohana 3和SimpleTest使用autorun.php
- 12. Kohana 3.x ORM has_many通过删除关系
- 13. 在Kohana 3 insert_id 3
- 14. Rails 3中的belongs_to和has_many
- 15. Kohana 3分页
- 16. 使用kohana关系(has_many等..)与kohana分页
- 17. 笨get_where和ORDER_BY
- 18. Kohana 2 ORM: - > with()for has_many relations
- 19. Kohana 3 - 比Kohana 2更容易吗?
- 20. Kohana的3 - 从URL
- 21. Kohana 3公告板
- 22. Kohana 3 - SphinxQL配置
- 23. Kohana 3 route not matching
- 24. Kohana 3 class name convention
- 25. Kohana 3 JavaScript问题
- 26. Kohana的3 - 构造
- 27. Rails 3 has_many通过3表
- 28. Kohana 3分页和路线问题
- 29. Kohana 3和“太多连接”错误
- 30. Kohana 3路由和查询字符串
它必须通过$ _has_many – sheeks06 2011-04-12 08:10:15
你是什么意思?在我的示例模型has_many items中,所以$ model-> items是一个has_many相关的对象。 – biakaveron 2011-04-12 10:09:33