Kohana 3.2-我想获得不同的日期 [英] Kohana 3.2 - i want to get distinct dates

查看:119
本文介绍了Kohana 3.2-我想获得不同的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个包含以下内容的表:

so i have a table with the following:

  1. id
  2. transaction_timestamp(TIMESTAMP CURRENT_TIMESTAMP)

我想使用Kohana ORM从交易时间戳中获取不同的日期 我有一个名为Model_Transactions

i would like to get distinct dates from the transaction timestamp using Kohana ORM i have a Model named Model_Transactions

如果我这样做

Model_Transactions::factory('transactions')->find_all();

我得到所有数据.由于我将其用作存档,因此我将如何仅获得不同的日期.我不希望用户单击存档并返回空白表.

i get all data. how would i get only the distinct dates since i would use it as an archive. i dont want the user to click on an archive and returns a blank table.

例如:用户单击"2012年4月1日",因为4月1日没有任何交易,因此有一个空白表,我想避免这种情况,怎么办?

for example: user clicks "April 01, 2012" since there is no transaction on April 01, there is a blank table, i would like to avoid those scenarios, how?

推荐答案

ORM模型应代表表中的单个条目.使用ORM find_all(),它将要选择表中的所有属性.由于您的ID应该是唯一的,因此每一行本质上都是不同的.

ORM models should represent a single entry in your table. Using the ORM find_all() it will want to select all attributes in the table. Since your id should be unique, each row is essentially distinct.

如果只想获取不同的transaction_timestamp值的列表,则可以在模型中添加一个方法来检索该值.像这样:

If you are just wanting to get a list of the distinct transaction_timestamp values, you could add a method to your model to retrieve just that. Something like:

public function get_distinct_timestamps()
{
    return DB::select('transaction_timestamp')
            ->from($this->_table_name)
            ->distinct('transaction_timestamp')
            ->execute()
            ->as_array();
}

然后您可以使用:

$timestamps = Model::factory('transactions')->get_distinct_timestamps();
foreach($timestamps as $timestamp)
{
    $value = $timestamp['transaction_timestamp'];
}

当然,这只是一个例子.您可能不想返回一个数组.但是您不限于使用ORM查询方法.

Of course this is just an example. You might not want to return an array. But you are not limited to using the ORM query methods.

获取具有不同transaction_timestamp的ORM对象的另一种方法是:

Another option to get ORM objects with distinct transaction_timestamp is to:

Model_Transactions::factory('transactions')
    ->group_by('transaction_timestamp')
    ->find_all();

这篇关于Kohana 3.2-我想获得不同的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆