Accessing Master table fields from Details in combined Add

My Master-Detail tables have been made fully uniform which refer two separate lookup tables. The basic design is like this (I’ve left out all unnecessary fields:

Classrooms lookup table:
–classroom_id
–classroom_name
–max_persons_in_room

Master table lists school Courses:
Courses Master table:
–course_id
–max_students_in_course

Details table consists of multiple Time_slots with room and time assignments for display in calendar eventually:
Timeslots Details table:
–course_id (linking to Master)
–classroom_id (lookup linking to Classrooms table)
–calculated value showing minimum value between max_students_in_course from Master and max_persons_in_room from Classrooms lookup automatically assigned, but which can be changed by user.

Calculated field on Timeslots table is done easily when records are Added separately after Courses data is assigned.

But how to calculate when new Master-Detail is created together ? What is the recommended way to access Master data fields from inside Detail?

The assignment can also be made in Row_inserting event. But I want the assignment made only once when classroom is first selected, and after that the user should be able to edit the value before saving record.

SRanade wrote:

What is the recommended way to access Master data fields from inside Detail?

Please try in “Row_Rendered” server event that belongs to Detail table:

// assume the Master table name is “master” and the field name you want to access in master table is “MasterID”
var_dump($GLOBALS[‘master’]->MasterID->CurrentValue);

I tried the following:

var_dump($GLOBALS[‘courses’]->course_id->CurrentValue);

on both Row_Rendered and Row_Rendering on server side of detail table. Both gave NULL values.

Possible work arounds:

  1. save the data from the Master field to a page-level or global variable, and then read the data from the Detail client-side event.

  2. read the field value from the display page from within the Detail client-side event.

I’m too new to JS / PHPMaker to figure out if this is even possible or how to do it. Any suggestions?

For your case, for v2021, please try this:

var_dump(Container(“courses”)->course_id->CurrentValue);

on both Row_Rendered and Row_Rendering on server side of detail table. Both gave NULL values.

Further experimenting on other fields reveals this:
– both Row_Rendered and Row_Rendering give the value of the initial field at the field, which includes any default assignments made before these events are called.
– the events are not called afterwards when I edit the Details field.

But what is needed is the current value of the field after changes have been made by user. I tried Row_Selecting but it does not display text through var_dump() so I don’t know if it is being called at all.

It should work if you open Master/Detail List page, Master/Detail Edit page, Master/Detail View page, and Master/Detail Copy (not Master/Detail Add) page.

Please put this code in “Row_Rendered” server event that belongs to the “orderdetails” table in demo project (v2021):

if ($this->getCurrentMasterTable() == “orders” && (CurrentPageName() == “orderdetailslist” || CurrentPageName() == “ordersedit” || CurrentPageName() == “ordersview” || (CurrentPageName() == “ordersadd” && !empty(Route(1))) ))
var_dump(Container(“orders”)->OrderID->CurrentValue);

Interesting observations and a workaround!

Observations:

var_dump(Container(“courses”)->course_id->CurrentValue);
This works on both Row_Rendered and Row_Rendering, but gives value of the last valid record in the table, and not the current ongoing record.

From my reading and understanding of MySQL: The actual data entered in the fields is not yet saved to a record, so any server side access will only give NULL or default values.

Workaround: I’ve managed a work-around like this:
Step 1. Declare a global variable in Client Scripts–>Global Code like this:
var my_global_var = “”; // Be careful to ensure that your variable name is unique and will not conflict with other page variables.

Step 2. in Master table create Client-side Event on the field that you want to copy, like this:
{ // keys = event types, values = handler functions
“change”: function(e) {
var $row = $(this).fields();
my_global_var = $row[“course_title”].value();
}
}

Step 3. in Detail table create Client-side Event when you want to copy, like this:
{ // keys = event types, values = handler functions
“change”: function(e) {
var $row = $(this).fields();
$row[“detail_course_title”].value(my_global_var ); //Or just use my_global_var as a variable in some calculation
}
}

And voila!

This is a just a workaround. Do please let me know if there is better/recommended way to do it. Thank you! :slight_smile:

Please put this code in “Row_Rendered” server event that belongs to the “orderdetails” table in demo project (v2021):

Hi, this is very useful code and I will make good use of it. It works perfectly when the Master record is already existing/saved and only the detail is being added/edited.But it does not work when Master is being created at the same time as the detail, for example at: ordersadd?showdetail=orderdetails.This is because the OrderID value (which is AUTO_INCREMENT) will be assigned only at the time when record is first inserted. Until then it is Null. See discussion here: stackoverflow.com/questions/2251463/mysql-php-how-to-get-auto-increment-field-value-before-inserting-dataThank you for your very helpful guidance.

You may actually get the last inserted id from “Row_Inserted” server event. For example, the auto increment field name is “id”, then simply put this following code in that event:

$this->setSuccessMessage("Last inserted id: " . $rsnew[“id”]);

Another approach is by optimizing the connection object to get the last inserted id, please see this topic: http://hkvforums.com/viewtopic.php?f=4&t=49285

Very useful. Thank you!

I needed to do the same thank you!
In the case of wanting to insert a sum from a detail field to a master field. Would it be the same methodology only using a query?
example: master: id, product, total_general detail: id, master_id, quantity
Where quantity from the detail table, then add the quantity and insert the result over total_general.