Page 1 of 1

Display concat fields in relationship

PostPosted: Thu Mar 25, 2010 10:15 am
by lenah
Greetings,

First of all, I want to say that Xataface is awesome! It's very flexible and provides so many options for customization. Bravo!

Now the question: I have a many-to-many relationships between two tables (person and assignment) and an associative table to store the relationship (person_assignment). Here is the simified table structure:
person(pid, firstname, lastname)
assignment(aid, description)
person_assignment(pid,aid)

I defined the relationship in my assignment folder using SQL and the relationship tab works fine.

In the details>edit tab, I use checkbox to allow users to assign the current assignment to multiple people. Currently, the field value for the checkbox is the person's first name. Is it possible to have a concat field value, such as concat(lname, " ", fname)?

Thanks!

Lena

Re: Display concat fields in relationship

PostPosted: Thu Mar 25, 2010 10:54 am
by shannah
If you implement the getTitle() method on the target table of the relationship to display both names, that should do the job:

Code: Select all
function getTitle(&$record){
    return $record->val('fname').' '.$record->val('lname');
}

Re: Display concat fields in relationship

PostPosted: Thu Mar 25, 2010 12:38 pm
by lenah
Hello Steve,

Thanks for your prompt reply. I have tried your solution. The field names are concatenated in the person tab but the checkboxs in assignment tab still display only the first name :(

This is how I declared my personname field in fields.ini in assignment folder:
[personname]
widget:type=checkbox
transient=1
relationship=personassignment

I declared personassignment in my relationships.ini:
[personassignment]
__sql__ = "SELECT * FROM person_assignment pa JOIN person p ON pa.pid = p.pid WHERE pa.aid = '$aid'"

I tried implemeting a delegate class on my assignment tab
<?php
class tables_assignment {
function personname__display(&$record){
return $record->val('firstname').' '.$record->val('lastname');
}
}
?>
But it still doesn't work :(

Re: Display concat fields in relationship

PostPosted: Thu Mar 25, 2010 12:49 pm
by shannah
So your relationship goes from the assignment table to the people table (i.e. it is showing the people that are related to a certain assignment).

Your getTitle() method should be defined in the people delegate class.

Re: Display concat fields in relationship

PostPosted: Thu Mar 25, 2010 1:03 pm
by lenah
This is my person.php
<?php
class tables_person {
function getTitle(&$record){
return $record->val('firstname').' '.$record->val('lastname');
}
}
?>

The title of the record does change to include both last name and first name but the field display of the check box does not :|

Re: Display concat fields in relationship

PostPosted: Thu Mar 25, 2010 1:14 pm
by shannah
OK.. it is possible that this is a case where you need to use the titleColumn() method instead.

The titleColumn has the same effect as getTitle() (it customizes the columns you mean to use as the title for a record). The difference is that titleColumn() is used when obtaining the title on an entire set of records without having to load the whole record into memory. It uses a different syntax also as it is meant to assist in SQL queries.

e.g.

Code: Select all
function titleColumn(){
    return "concat(firstname,' ',lastname)";
}


It should return a string that can be used as a column identifier in the select clause of an SQL query.

(This method goes in your people delegate class).

-Steve

Re: Display concat fields in relationship

PostPosted: Thu Mar 25, 2010 1:20 pm
by lenah
Hello Steve,

Thank you so much! It works like a charm!

Cheers,
Lena