Kohana 3.2 - i want to get distinct dates

夙愿已清 提交于 2019-12-12 05:03:54

问题


so i have a table with the following:

  1. id
  2. transaction_timestamp (TIMESTAMP CURRENT_TIMESTAMP)

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

if i do this

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.

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?


回答1:


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.

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();
}

Then you can use:

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

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.

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

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


来源:https://stackoverflow.com/questions/10002613/kohana-3-2-i-want-to-get-distinct-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!