Php 科哈纳3:你有很多,而且是按顺序订购的吗
如何在Kohana 3中使用has\u many关联的查询排序?根据Php 科哈纳3:你有很多,而且是按顺序订购的吗,php,mysql,kohana,kohana-3,kohana-orm,Php,Mysql,Kohana,Kohana 3,Kohana Orm,如何在Kohana 3中使用has\u many关联的查询排序?根据Kohana\u ORM::\u get()实现-您不能 它所做的只是编写where条件,而不可能添加排序: elseif (isset($this->_has_many[$column])) { $model = ORM::factory($this->_has_many[$column]['model']); if (isset($this->_has_ma
Kohana\u ORM::\u 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
,然后在那里重新实现。您需要重写我上面给出的部分(如果isset($this->\u有许多[$column])
),或者将控件传递给父项::\uu get($column)
)。在这种情况下,您可以自由地向\u添加多个参数,如order\u 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;
}
}
根据Kohana\u ORM::\u 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
,然后在那里重新实现。您需要重写我上面给出的部分(如果isset($this->\u有许多[$column])
),或者将控件传递给父项::\uu get($column)
)。在这种情况下,您可以自由地向\u添加多个参数,如order\u 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;
}
}
您是否尝试过类似于$model->items->order_by('fieldname')->find_all()
\uu get()
方法返回查询生成器对象,而不是数据库结果,因此您可以根据需要添加QBuilder的条件(where/order\u by/etc) 您是否尝试过类似于$model->items->order\u by('fieldname')->find\u all()
\uu get()
方法返回查询生成器对象,而不是数据库结果,因此您可以根据需要添加QBuilder的条件(where/order\u by/etc) @sheeks06:不是内置的,但你可以用自己的轻松扩展。@sheeks06:不是内置的,但你可以用自己的轻松扩展。你是什么意思?在我的例子中,模型有很多项,所以$model->items是一个有很多相关的对象。你是什么意思?在我的示例中,模型有许多项,因此$model->items是一个有许多相关对象的对象。