问题
so i have a table with the following:
- id
- 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