Sort related records

Archived from the Xataface Users forum.

mhassl — Wed Jan 23, 2013 8:20 pm

I need a way to have the contact log table which is the child of the contact logs table to have a sort other than the primary key of the logs table. I have tried the addition to the relationships.ini file:

[contact_log]
contact_log.contact_id = “$Contact_Id”
metafields: order = log_date desc

as well as the ApplicationDelegate.php:

<?php
class conf_ApplicationDelegate {
function getPermissions(&$record){
// $record is a Dataface_Record object
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
if ( $user ) return Dataface_PermissionsTool::ALL();
else return Dataface_PermissionsTool::NO_ACCESS();
}
function beforeHandleRequest(){
//$query =& Dataface_Application::getInstance()->getQuery();
$app =& Dataface_Application::getInstance();
$query =& $app->getQuery();

//if ( $query[‘-table’] == ‘tbl_jobs’ and !@$query[‘-sort’] and !$_POST ){
// $query[‘-sort’] = ‘job_id desc’;
//}
if( !$_POST and $query[‘-table’] == ‘contacts’ and !isset($query[‘-sort’]) ){
$query[‘-sort’] = ‘Last_Name asc, First_Name asc’;
}
if( !$_POST and $query[‘-table’] == ‘contact_log’ and !isset($query[‘-sort’]) ){
$query[‘-sort’] = ‘log_date desc’;
}

}
}

Neither works. What is the correct technique? This should be a common and simple task.

Thanks,
Mike


shannah — Thu Jan 24, 2013 9:48 am

Sorting related records is done via the -related:sort parameter, rather than -sort.

So if you wanted to set a default sort for the “children” relationship of the “parents” table, you might do something like.

Code: Select all
if ( !@$_POST and $query['-table'] == 'parents' and $query['-relationship'] == 'children'  and !@$query['-related:sort'] ){     $query['-related:sort'] = 'name desc'; }

Or something like that.

-Steve


mhassl — Sun Feb 03, 2013 4:34 pm

Steve,

Thanks the sort works but I get the following error at the top of the screen upon login:

Notice: Undefined index: -relationship in C:\Program Files (x86)\EasyPHP-5.3.9\www\habitat\conf\ApplicationDelegate.php on line 17

The file contains the following:

<?php
class conf_ApplicationDelegate {
function getPermissions(&$record){
// $record is a Dataface_Record object
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
if ( $user ) return Dataface_PermissionsTool::ALL();
else return Dataface_PermissionsTool::NO_ACCESS();
}
function beforeHandleRequest(){
//$query =& Dataface_Application::getInstance()->getQuery();
$app =& Dataface_Application::getInstance();
$query =& $app->getQuery();
if( !$_POST and $query[‘-table’] == ‘contacts’ and !isset($query[‘-sort’]) ){
$query[‘-sort’] = ‘Last_Name asc, First_Name asc’;
}
if ( !@$_POST and $query[‘-table’] == ‘contacts’ and $query[‘-relationship’] == ‘contact_log’ and !@$query[‘-related:sort’] ){
$query[‘-related:sort’] = ‘log_date desc’;
}
}
}

The log_date and all the other required indices exist. So what does the error mean, and how do I fix it?

Here is the relationship.ini for contacts table:

[contact_log]
contact_log.contact_id = “$Contact_Id”

[contact_info]
contact_info.contact_id = “$Contact_Id”

Thanks,
Mike


shannah — Mon Feb 04, 2013 9:48 am

Since ‘-relationship’ is only included in some requests you need to suppress errors… e.g.

Code: Select all
@$query['-relationship']

instead of

Code: Select all
$query['-relationship']

-Steve