Page 1 of 1

Send field data of the main table from the child table

Posted: 2020-11-19 12:14
by yonder
Hi there,

I have a project and it has 2 tables;

Table_1 : id, project_no, project_name
Table_2 : id, project_no(lookup from table_1), comment

First i add two records to table_1;
id=1
project_no = 1234
project_name = hello

id=2
project_no = 5555
project_name =bye

Then i add a record to table_2;
id=5
project_no = 1234 (selected from lookup)
comment = it's good

I added following codes to table_2 hookup file;

Code: Select all

function table_2_after_update($data, $memberInfo, &$args) {

	if($data['comment'] <> '')  {
                         sendmail(array(
                         'to' => '[email protected]',
                         'name' => 'Tester',
                         'subject' => 'A comment added to project',
                        'message' => 'Hi, there is a new comment<br>Comment : ' . $data['comment'] . '<br>Project No : ' . $data[project_no]
                                ));
                        };
                }
		return TRUE;
	}
It's send email but i see the project no of the table_2 as id but i want to see the project number from table_1.

How can i do that?

Best Regards.

Re: Send field data of the main table from the child table

Posted: 2020-11-19 20:47
by pbottcher
Hi,

try

Code: Select all

function table_2_after_update($data, $memberInfo, &$args) {

	$rec=get_joined_record('table_2',$data['selectedID']);

	if($data['comment'] <> '')  {
                         sendmail(array(
                         'to' => '[email protected]',
                         'name' => 'Tester',
                         'subject' => 'A comment added to project',
                        'message' => 'Hi, there is a new comment<br>Comment : ' . $data['comment'] . '<br>Project No : ' . $rec['project_no']
                                ));
                        };
                }
		return TRUE;
	}

Re: Send field data of the main table from the child table

Posted: 2020-11-20 05:30
by yonder
pböttcher wrote:
2020-11-19 20:47
Hi,

try

Code: Select all

function table_2_after_update($data, $memberInfo, &$args) {

	$rec=get_joined_record('table_2',$data['selectedID']);

	if($data['comment'] <> '')  {
                         sendmail(array(
                         'to' => '[email protected]',
                         'name' => 'Tester',
                         'subject' => 'A comment added to project',
                        'message' => 'Hi, there is a new comment<br>Comment : ' . $data['comment'] . '<br>Project No : ' . $rec['project_no']
                                ));
                        };
                }
		return TRUE;
	}
pböttcher, you're the best. Thank you so much.

Another question;

Code: Select all

if($data['comment'] <> '')
It's checking there is a comment or not. However if i add a comment and save it send email, after that if i edit the record, do nothing and save it again, it send the same email because the comment already inserted before, i mean it's not empty. How can i solve it?

Thanks a lot.

Re: Send field data of the main table from the child table

Posted: 2020-11-20 19:58
by pbottcher
Hi,

do you need this in the after update? Or can you also use it in the before update? That means is there a risk that the update may fail due to any restriction?

Re: Send field data of the main table from the child table

Posted: 2020-11-22 20:45
by yonder
pböttcher wrote:
2020-11-20 19:58
Hi,

do you need this in the after update? Or can you also use it in the before update? That means is there a risk that the update may fail due to any restriction?
Hi pböttcher,

Yes, you're right, i will move it to before update.

Re: Send field data of the main table from the child table

Posted: 2020-11-22 21:26
by pbottcher
If you use it in the before update, you can retrieve the actual value stored in the database and compare it against the one set in the data array.
If they do match, you do not need you mail, otherwise send your mail.

Re: Send field data of the main table from the child table

Posted: 2020-11-24 09:12
by yonder
pböttcher wrote:
2020-11-22 21:26
If you use it in the before update, you can retrieve the actual value stored in the database and compare it against the one set in the data array.
If they do match, you do not need you mail, otherwise send your mail.
So could you prepare an example for me please?

Re: Send field data of the main table from the child table

Posted: 2020-11-24 10:48
by yonder
yonder wrote:
2020-11-22 20:45
pböttcher wrote:
2020-11-20 19:58
Hi,

do you need this in the after update? Or can you also use it in the before update? That means is there a risk that the update may fail due to any restriction?
Hi pböttcher,

I remember why i used after update option, because i send a link for the record with email, so i need the id of the record, so i used after update.