Page 1 of 1

Assigning descending order to related records

PostPosted: Mon Sep 17, 2012 11:22 am
by sworden
I found this article pertaining to assigning order to related records:
http://xataface.com/documentation/how-t ... ed-records

Andthe code works just fine, but how do I get the records to show up in descending order? Here is my code:
Code: Select all
[transcripts]
transcripts.APPLICANTS_ID = "$APPLICANTS_ID"
metafields:order = "transcripts.TRANSCRIPTS_DATE"

Re: Assigning descending order to related records

PostPosted: Mon Sep 24, 2012 9:25 am
by shannah
The metafields:order isn't really intended to be used this way. It is intended to be a numeric field that will be updated automatically to reflect the user assigned ordering of records. This column would then be the default sort order.

That said, your example will still result in a default sort on your date column... it just won't behave properly when the user tries to manually reorder the records. Because this wasn't the intended usage, there isn't a "descending" or other simple directive to cause it to sort in reverse.

There are two possible solutions to your problems.

1. Manipulate the query inside the application delegate's beforeHandleRequest() method to add the appropriate GET parameter to cause the records to be sorted descending on that column. (Try manually sorting the records on a column and take a look at the GET parameters in the resulting request to see how Xataface handles this and you'll know what parameter needs to be added).

2. Create a grafted field on the related table that is calculated such that it has a lower value for more recent dates and a higher value for older dates. Then use the metafields:order directive on this field.

-Steve

Re: Assigning descending order to related records

PostPosted: Thu Nov 01, 2012 8:41 am
by sworden
Thanks!

I've created a calculated field by adding this to my dates.php file. It creates a negative number, with the more current date having a lower value than an older date. There is probably a better way to do this, but it works:
Code: Select all
#This updates the DATES_SORT field.
mysql_query("UPDATE dates SET DATES_SORT = 100-STEP_DATE");

And in my relationships.ini file I now have this code to sort based on the calculated field, DATES_SORT:
Code: Select all
[progress]
dates.APPLICANTS_ID = "$APPLICANTS_ID"
metafields:order = "dates.DATES_SORT"

How do I get the DATES_SORT field to be hidden in all views? I'm the only one who's ever going to view the field, and I can do that in phpMyAdmin.

Re: Assigning descending order to related records

PostPosted: Thu Nov 01, 2012 8:49 am
by shannah
Code: Select all
visibility:list=hidden
visibility:browse=hidden
visibility:find=hidden
widget:type=hidden

Re: Assigning descending order to related records

PostPosted: Thu Nov 01, 2012 9:29 am
by sworden
Thanks! I had tried a few of those, but not all of them. That did it.