如何订购在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 has_many relationship
- 2. kohana - 帮助has_many
- 3. Adodb和Kohana 3
- 4. Kohana 3.1 ORM:加入has_many
- 5. 在Kohana 3 insert_id 3
- 6. Rails 3中的belongs_to和has_many
- 7. Kohana 3 arr :: rotate
- 8. Kohana 3分页
- 9. Kohana 2 ORM: - > with()for has_many relations
- 10. Kohana 3 - SphinxQL配置
- 11. Kohana的3 - 从URL
- 12. Kohana的3 - 构造
- 13. Kohana 3公告板
- 14. Kohana 3 JavaScript问题
- 15. Kohana 3 route not matching
- 16. Kohana 3路由和查询字符串
- 17. Kohana 3分页和路线问题
- 18. Kohana 3和“太多连接”错误
- 19. group_by和order_by与codeigniter
- 20. 在kohana 3中实现uploadify 3
- 21. Kohana 3中的PHP会话3
- 22. 在Kohana 3制作水印3
- 23. Kohana 3中的基本URL 3
- 24. Kohana 3中的i18n和错误消息3
- 25. Kohana 3:验证模块
- 26. Kohana 3简单的关系
- 27. Kohana 3.x验证码
- 28. Kohana 3控制器构造
- 29. Kohana 3 - 获取网址
- 30. Kohana 3在PHP 5.1.6下
它必须通过$ _has_many – sheeks06 2011-04-12 08:10:15
你是什么意思?在我的示例模型has_many items中,所以$ model-> items是一个has_many相关的对象。 – biakaveron 2011-04-12 10:09:33