get calculated field from parent table [message #1947] |
Mon, 09 February 2009 13:23 ![Go to next message Go to next message](/theme/default/images/down.png) |
ikatz
Messages: 40 Registered: December 2007 Location: Durham, NH
|
Member |
|
|
I have a table of people called "person", which lists contact information. I have a table called "staff", which associates a person_id with some job-specific data (their position, work phone number, etc).
For my staff table, Radicore's dictionary calculates a person_name from from the foreign key person_id, and this name appears in all the staff screens instead of the number. This is good.
I would also like to use the person_name value in place of staff_id for all the screens that have staff_id as a foreign key. But, the dict file alone can't do this. What is the best way to accomplish this in Radicore?
I assume I have to do something with an instance of the person table in the _cm_getExtraData method of the staff table's class... but I can't seem to figure it out.
|
|
|
Re: get calculated field from parent table [message #1948 is a reply to message #1947] |
Mon, 09 February 2009 17:29 ![Go to previous message Go to previous message](/theme/default/images/up.png) ![Go to next message Go to next message](/theme/default/images/down.png) |
AJM
Messages: 2361 Registered: April 2006 Location: Surrey, UK
|
Senior Member |
|
|
Let's see if I've got this straight - you have a table (let's call it 'x') with a foreign key called 'staff_id' which points to an entry on the 'staff' table. The 'staff' table contains a foreign key called 'person_id' which points to an entry on the 'person' table. The 'person' table contains a column called 'person_name'.
This means that when you read a row from table 'x' you want to read table 'staff' then table 'person' in order to fetch the 'person_name' column.
In this case the relationship between the 'person' table and the 'staff' table should be defined in the dictionary so that 'person_name' is the parent field. This means that when an occurrence of the 'staff' table is retrieved the framework will automatically JOIN to the parent 'person' table and bring back the value for 'person_name'.
In your _cm_getExtraData() method for table 'x' you will need code simiar to the following:
if (empty($fieldarray['person_name']) AND !empty($fieldarray['staff_id'])) }
$dbobject =& singleton::getInstance('staff');
$data = $dbobject->getData("staff_id='{$fieldarray['staff_id']}'");
$fieldarray['person_name'] = $data[0]['person_name'];
} // if
Try it and see.
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
|
|
|
|
|