Kohana Select order by - kohana

Sometimes when I refresh page the events I have sorts them DESCENDING by date, but I need ascending order by time.
http://prntscr.com/bubuz5
http://prntscr.com/bubvad
Code
public function get_frontpage_events()
{
return DB::select()->
from('events')->
where_open()->
where('frontpage', '=', 1)->
where('status', '=', 1)->
where('lang', '=', Session::instance()->get('lang'))->
where_close()->
order_by('date', 'ASC')->order_by('time', 'ASC')->
execute()->
as_array();
}

You query looks fine.
I don't know if you have a query log, but if you have you can check what query is executed, copy and paste it and run it yourself. The above code should create this SQL query (my guess is you are using MySQL):
SELECT * FROM `events` WHERE (`frontpage` = 1 AND `status` = 1 AND `lang` = 'EN') ORDER BY `date` ASC, `time` ASC
Do you do some other ordering/sorting in your view or your controller? When you loop over your results, are you sure you display all results?

The problem can be in order_by()->order_by() syntax. This method allows to use array in first argument, try this:
order_by(array('date','time'), 'ASC')

Related

Writing a subquery to display records in a grid

I have two DAC's POReceipt, and and POReceiptLine. POReceiptLine containts a field called MfrPartNbr.
I want the user to be able to lookup all the POReceipts where the POReceiptLine.MfrPartNbr is equal to an entered value.
The SQL would be
SELECT *
FROM dbo.POReceipt
WHERE POReceipt.ReceiptNbr IN
(
SELECT ReceiptNbr
FROM dbo.POReceiptLine
WHERE MfrPartNbr = 'MY_ENTERED_PART_NBR'
)
Any idea how to write the BQL Statement for this?
As stated, an inner join won't work in this case because you will receive the same POReceipt multiple times (once for each POReceiptLine). The following BQL query shows how you can get the desired results using a sub query. If mfrPartNbr is an extension field, then replace POReceiptLine.mfrPartNbr with the correct extension name (e.g. POReceiptLineExtension.mfrPartNbr).
PXSelect<POReceipt, Where<Exists<
Select<POReceiptLine,
Where<POReceiptLine.receiptNbr, Equal<POReceipt.receiptNbr>,
And<POReceiptLine.mfrPartNbr, Equal<Required<POReceiptLine.mfrPartNbr>>>>>>>>.Select(this, "MY_ENTERED_PART_NBR");

How to filter on view using complex query

I am trying to filter on a view which emits bookName and bookItem.
emit([doc.basicInfo.bookName,doc.basicInfo.bookItem], 1);
it gives me below result without any query:
{"total_rows”:10,”offset":0,"rows":[
{"id":"d4e5548fb01e6e2c559e702fe7b138ad","key":["correctaccouts","billing"],"value":1},
{"id":"863c46c645b6344719a08231606f2a7d","key":["credeaccount","system"],"value":1},
{"id":"68d39e64c406127960dc735e8167eee3","key":["credeaccount11","system"],"value":1},
{"id":"1ab4d31588d76a42e85b526a316074de","key":["mayankamazon","billing"],"value":1},
{"id":"3204f5db5df91886373f95995ce09a2d","key":["mayankazure","asset"],"value":1},
{"id":"452c040048fb2b779205b3785615d368","key":["mayankmaaa","system"],"value":1},
{"id":"23f01f7bc60c2c8f24f6b741584a69fa","key":["TEST_AWS_Delete212sss12","asset"],"value":1},
{"id":"f0093f474e0d50f046b9fdc9145bdc91","key":["vijeth-myteam111115555555","asset"],"value":1},
{"id":"c3bce8dd1482d841f445fbd617ba1db7","key":["vijeth-myteam11111555sss5555","asset"],"value":1},
{"id":"347479ba91696b73f4a57252cd00a358","key":["vijeth-myteamOnly","asset"],"value":1}
]}
Now I am trying to query on it using complex keys:
satrtkey=[{},"asset"]&endkey=[{},"asset"]
It should return me:
{"total_rows”:5,”offset":0,"rows":[
{"id":"3204f5db5df91886373f95995ce09a2d","key":["mayankazure","asset"],"value":1},
{"id":"23f01f7bc60c2c8f24f6b741584a69fa","key":["TEST_AWS_Delete212sss12","asset"],"value":1},
{"id":"f0093f474e0d50f046b9fdc9145bdc91","key":["vijeth-myteam111115555555","asset"],"value":1},
{"id":"c3bce8dd1482d841f445fbd617ba1db7","key":["vijeth-myteam11111555sss5555","asset"],"value":1},
{"id":"347479ba91696b73f4a57252cd00a358","key":["vijeth-myteamOnly","asset"],"value":1}
]}
But it still gives me all 10 records. I want to filter only records of type "asset".
To use key ranges, you must narrow down your research starting with the left fields to the right fields.
For example, if your key would be: [doc.basicInfo.bookItem,doc.basicInfo.bookName]
You could search with start_key=["asset",null]&end_key=["asset",{}]
Also, your current query is equivalent to key=[{},"asset"]. Instead, you should have tried: start_key=[null,"asset"]&end_key=[{},"asset"] but it should not work.
Example
View:
function (doc) {
emit([doc.basicInfo.bookItem,doc.basicInfo.bookName], 1);
}
Query:
http://localhost:5984/<db>/_design/<design_name>/_view/<view_name>?include_docs=true&inclusive_end=true&start_key=%5B%22asset%22%2Cnull%5D&end_key=%5B%22asset%22%2C%7B%7D%5D

Getting index of the resultset

Is there a way to get the index of the results within an aql query?
Something like
FOR user IN Users sort user.age DESC RETURN {id:user._id, order:{index?}}
If you want to enumerate the result set and store these numbers in an attribute order, then this is possible with the following AQL query:
LET sorted_ids = (
FOR user IN Users
SORT user.age DESC
RETURN user._key
)
FOR i IN 0..LENGTH(sorted_ids)-1
UPDATE sorted_ids[i] WITH { order: i+1 } IN Users
RETURN NEW
A subquery is used to sort users by age and return an array of document keys. Then a loop over a numeric range from the first to the last index of the that array is used to iterate over its elements, which gives you the desired order value (minus 1) as variable i. The current array element is a document key, which is used to update the user document with an order attribute.
Above query can be useful for a one-off computation of an order attribute. If your data changes a lot, then it will quickly become stale however, and you may want to move this to the client-side.
For a related discussion see AQL: Counter / enumerator
If I understand your question correctly - and feel free to correct me, this is what you're looking for:
FOR user IN Users
SORT user.age DESC
RETURN {
id: user._id,
order: user._key
}
The _key is the primary key in ArangoDB.
If however, you're looking for example data entered (in chronological order) then you will have to have to set the key on your inserts and/or create a date / time object and filter using that.
Edit:
Upon doing some research, I believe this link might be of use to you for AI the keys: https://www.arangodb.com/2013/03/auto-increment-values-in-arangodb/

How to get last insert id when using sub query in Kohana

How do I return the correct $insert_id when using a sub query in Kohana?
I'm using the query method to return $insert_id and $affected_rows. It returns the correct value for $affected_rows but returns '1' for $insert_id which is incorrect.
Query below:
$sub = DB::select('id', 'username', 'email', 'lastVisitDate')->from('jos_users');
$qry_migrate_users = DB::insert('temp_users', array('old_id', 'username', 'email_work', 'last_login'))->select($sub);
list($insert_id, $affected_rows) = $qry_migrate_users->execute($this->conn_target);
MySQL returns only last insert id and affected rows. there is only one way to do what you want - execute your sub-select to array, and using foreach do single inserts. But it's a lilte bit slower operation! Or after inserting do something like that:
SELECT id FROM temp_users WHERE email IN (select email from jos_users)
You might understand the logic

expressionengine database class orderby and sort

How do we sort using the database class in expressionengine. orderby and sort are given an error and do not seem to work. I can't seem to find anything in the documantation about sorting results. This is what i have.
$results = $this->EE->db->query("
SELECT plan_name
FROM exp__plans
WHERE member_id='1002' AND orderby="id" sort="desc" LIMIT 1
");
$x = $results->row('plan_name')
;
There are issues with your query.
try:
$results = $this->EE->db->query("
SELECT plan_name
FROM exp_plans
WHERE member_id = '1002'
ORDER BY id DESC LIMIT 1
");
I would recommend trying to run your query directly against the database if you're having trouble with it. 90% of the time it will be an issue with your SQL.
Also, you are writing this in a add-on... right? if you're trying to get this to work within a template I would recommend checking out the query module.
You can also use Active Record in order to create your query:
$this->EE->db->select('plan_name')
->from('plans')
->where('member_id', '1002')
->order_by("id", "desc")
->limit(1)
->get();
All the doc is on the Codeigniter website.

Resources