Radicore Forum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » RADICORE » How To » get calculated field from parent table
get calculated field from parent table [message #1947] Mon, 09 February 2009 13:23 Go to next message
ikatz is currently offline  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 messageGo to next message
AJM is currently offline  AJM
Messages: 2371
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.


Re: get calculated field from parent table [message #1949 is a reply to message #1948] Mon, 09 February 2009 19:48 Go to previous messageGo to next message
ikatz is currently offline  ikatz
Messages: 40
Registered: December 2007
Location: Durham, NH
Member
Aah, that works perfectly.

My mistake was in trying to do this from the staff table's class.
Re: get calculated field from parent table [message #1950 is a reply to message #1949] Tue, 10 February 2009 05:10 Go to previous message
AJM is currently offline  AJM
Messages: 2371
Registered: April 2006
Location: Surrey, UK
Senior Member
It's no good putting the right code in the wrong place. Sad

The big trick is knowing what code to put where so that it gets executed at the right time. Once you have mastered that you will find that anything is possible with the Radicore framework. Smile


Previous Topic: How to make a list2 on a non existing child table
Next Topic: Dynamically generate pdf layout?
Goto Forum:
  


Current Time: Fri Nov 29 05:46:42 EST 2024

Total time taken to generate the page: 0.02704 seconds