This topic is locked

Master-detail: This should be a common ADD scenario, or not?

1/3/2024 2:26:18 PM
PHPRunner General questions
S
spin author

Hi all.
My case is rather simple:
I have an ADD form that stores data to both a master and a detail (one-to-many) table.
All i want is the system to send an email containing both master record data and all detail records data.
So far i have figured out that the AfterRecordAdded of the master table is executed first, and then the AfterRecordAdded of the detail, one time for each record.
If i use the event of the master, i lose the data of the details.
And i dont know how many detail records will be added, if any.
Is there a way to overcome this problem?

admin 1/3/2024

Like I said before, the only way to do that is to use beforeSave event of master record, go through the whole list of records to be added and do something with the result:
https://xlinesoft.com/phprunner/docs/how_to_ask_for_confirmation_before_saving_record.htm

S
spin author 1/29/2024

Thanks for the reply, Sergey.
We are having a hard time to figure out how to access the added records of the detail table, since the mentioned example does not access any fields.
If in the ADD page we have a record of the master table called "MASTER" and a few fresh records of detail table named "DETAIL", how can we iterate through the detail values?
We'd appreciate it if you could give us 2-3 code lines as a sample, just to push us to the right direction.
Thanks in advance!

fhumanes 1/30/2024

Hi @spin:

In an example of mine from Master-Detail, I have included the trace that I explain in this article and that serves to debug applications.
https://fhumanes.com/blog/guias-desarrollo/guia-34-metodo-basico-para-depuracion-codigo/

I have since I built a trace in the After Added of the Master (Invoice) and the Detail (Invoice Line) and this is the trace that generates me:

[Tue, 30 Jan 2024 12:12:22 +0100] Error 1: URL ejecutada: /invoice/factura_add.php
[Tue, 30 Jan 2024 12:12:22 +0100] Error 1: URL ejecutada: /invoice/linea_factura_add.php
[Tue, 30 Jan 2024 12:12:25 +0100] Error 1: URL ejecutada: /invoice/autofillfields.php
[Tue, 30 Jan 2024 12:12:28 +0100] Error 1: URL ejecutada: /invoice/autofillfields.php
[Tue, 30 Jan 2024 12:12:33 +0100] Error 1: URL ejecutada: /invoice/linea_factura_add.php
[Tue, 30 Jan 2024 12:12:36 +0100] Error 1: URL ejecutada: /invoice/autofillfields.php
[Tue, 30 Jan 2024 12:12:42 +0100] Error 1: URL ejecutada: /invoice/factura_add.php?page=add&submit=1&
[Tue, 30 Jan 2024 12:12:42 +0100] Error 1: URL ejecutada: /invoice/linea_factura_add.php?submit=1&inline=1&
[Tue, 30 Jan 2024 12:12:42 +0100] Error 3: After linea : 64
[Tue, 30 Jan 2024 12:12:42 +0100] Error 1: URL ejecutada: /invoice/linea_factura_add.php?submit=1&inline=1&
[Tue, 30 Jan 2024 12:12:42 +0100] Error 3: After linea : 65
[Tue, 30 Jan 2024 12:12:42 +0100] Error 1: URL ejecutada: /invoice/factura_add.php?afteradd=3ot6c3dxu03dgdscypce
[Tue, 30 Jan 2024 12:12:42 +0100] Error 2: After factura: 20
[Tue, 30 Jan 2024 12:12:42 +0100] Error 1: URL ejecutada: /invoice/factura_edit.php?editid1=20&

As you can see, the Master's After Added event is the last thing that runs.

You can do this same test with your example.

Greetings,
fernando

S
spin author 1/30/2024

Hi @fhumanes, thanks for the reply.

My code (I simplified it) looks like this:

function Master_AddPage_AfterRecordAdded($values)
{
SendPdf($values);
}

function SendPdf($master_data)
{
$html = GenerateHtml($master_data);
// here $html is converted to PDF and sent via e-mail
}

function GenerateHtml($master_data)
{
$rs = DB::Query("SELECT * FROM details WHERE fk=" . $master_data["id"]);
while ($detrec = $rs->fetchAssoc())
{
// process details record
}
// return value
}

This code never finds any detail records, indicating that they are always added to the database after the AfterRecordAdded event of the master table.
I tried to follow Sergey's approach, but I was not able to find a way to access the detail records from javascript and then pass it to php.

admin 1/31/2024

The correct approach here is to redirect the user to the View page of the record that was just added. On that page you can have access to both master and details data.

If you want to access details data right on the Add page in Javascript, you need to use jQuery. I attach a couple of screenshots that explain what you are looking for.

img alt

img alt

S
spin author 2/1/2024

Details data processed successfully, thanks @admin!
All the work is done in the "JavaScript Onload event" by binding the "afterSave" event of the master's ADD page.
There is only one last issue left to address:
In this case, does PHPRunner provide a direct way to pass the data to an internal php event (like "proxy"), in order to create the pdf and send the email?
Or should we follow the traditional way of calling a custom php file via http?