How does the onDeleteRowsForm trigger works?

mirceat

Member
I need to do this: delete records from another table when a specific row is deleted. Something like:

if $id no longer exist in table_A then "delete from table_B where id = $id"

I was thinking maybe i can use the onDeleteRowsForm trigger, but i don't know the syntax code and i couldn't find this info in forum.

Thank you
 
P.S. Once you have made this work, if you fancy updating the wiki with documentation for the onDeleteRowsForm trigger and with an example of this type of cascading delete, that would be great.
 
Thank you, Sophist, but my problem is how to get the value of the $id onDeleteRowsForm .

I tried every

echo"<pre>";print_r($formModel->data);exit;

echo"<pre>";print_r($formModel->formData);exit;

echo"<pre>";print_r($data);exit;

but it doesn't display anything except the success message for deleting the record.
 
Can you try:
PHP:
echo "<pre>";print_r('{rowid}');exit;
to see if that gets you the id you need?
 
Nope, still nothing. Maybe i'm doing something wrong. Steps to replicate:

1. Edit admin form
2. Add php plugin
3. Set Process script to "On record deletion.."
4. Paste code echo "<pre>";print_r('{rowid}');exit;
5. Go to frontend and delete an existing record. Expecting: rowid value Actual response: 1 record deleted.
 
As far as I can see any documention on "onDelete" is still missing.

I did it some time ago with the help of Rob.
In a form php onDelete I do
foreach ($data[0][0] as $row)
{
//my code;
}

which is triggered by a delete in list view, too.
In the form it's exactly one row; if triggered by list view with multiple records selected for deletion it's a "real" loop.

So I assume it's the same/similar to what would be in list php_events.

I hope it is still working this way (the site is active and the customer did not complain until now;)).
It would be great if you get it sorted out and put it into the WIKI.
 
I should mentioned that i'm trying to delete the records in form, not in list view.

Anyway, this code works for rowid, but i can't make it work for other elements in form:

PHP:
foreach ($data[0][0] as $row)
{
var_dump({rowid});exit; //works, will display int(2)
}

this will display NULL
var_dump($row->my_element);exit;
var_dump($row->{'my_element'});exit;
var_dump($row->my_element[0]);exit;
var_dump(json_decode($row, true));exit;
 
This code IS from my form php plugin (but also running if deleting from list view, without any list plugin...)

$row->full-element-name should do.
You can var_dump($row);
 
PHP:
foreach ($data[0][0] as $row)
{
var_dump({rowid});exit;//works, will display int(2)
}
is the same as:
PHP:
var_dump({rowid});exit;
"exit" means ... um ... well ... exit. So it exits on the first iteration of the loop. And since $row is not used in the loop, you might as well not have it.

Why not try:
PHP:
foreach ($data[0][0] as $row)
{
var_dump($row);
}
exit;
or:
PHP:
var_dump($formModel->data);
var_dump($formModel->formData);
var_dump($data);
exit;
 
The onDeleteRowsForm hook is called from the main list model deleteRows() method, which is called both when you do a list delete (potentially with multiple rows) and also from form controller when you delete from a form view.

In both case, the form PHP plugin's onDeleteRowsForm() method is triggered, with the row data in $data, which is an array of arrays (groups) of objects (rows). So you should always code it assuming that you may get called with multiple rows (deleting from a list will do that). Even if it's being called in the form context, it'll still be a two dimensional array of objects, it'll just be a single group with a single row in it.

So ...

Code:
foreach ($data[0] as $group) {
   foreach ($group as $row) {
      // your rowid will be in $row->__pk_val
      // elements will be in $row->tablename___fieldname
   }
}

Don't use {rowid}, as that's just a placeholder for the current row in form view. So it'll work if your code is running from a delete in a form context, but won't work if it's being called from a list delete. And as explained, you have to assume your code will get called from a list delete - the same plugin is being triggered. So use $row->__pk_val.

For element data, use full element names, like $row->tablename___fieldname, or with the _raw suffix, $row->tablename___fieldname_raw (if your element is one with the concept of "value" and "label", like join elements).

If anyone wants to summarize that into the wiki, I'd appreciate it.

-- hugh

Edit @troester 2021-02-11: corrected to $data[0] (see @mirceat)
 
Last edited by a moderator:
I might get it wrong, but using this code and trying to display values from form or list view will works:

PHP:
foreach ($data[0][0] as $row)
{
var_dump($row->__pk_val);//return string(1) "3"
var_dump($row->test___clasa_raw);//return string(3) "307"
}
exit;

but this one doesn't return desired values:

PHP:
foreach ($data as $group)
{
  foreach ($group as $row){
    var_dump($row->__pk_val); //return NULL
    var_dump($row->test___clasa_raw); //return NULL
  }
}
exit;
 
Oh yeah, sorry, everything gets wrapped up in an additional args array, so ...

$data = $data[0];

... as the first line should fix it.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top