I'm having problems using the server-side postEdit event (in PHP). In the first case, I'm using the postCreate event for a row in one table to insert a new record into each of three other tables in the same database. The event handler calls the function insertNewUserRecords that writes the records in the other tables. This works just fine. The code excerpt is here:
// Here we need to insert a new record into the related tables when creting a new user
function insertNewUserRecords($db, $row) {
$db->insert('adminGroups', array(
'email' => $row['email'],
'updated' => date('Y-m-d H:i:s')
) );
$db->insert('docViewGroups', array(
'email' => $row['email'],
'updated' => date('Y-m-d H:i:s')
) );
$db->insert('preferences', array(
'email' => $row['email'],
'updated' => date('Y-m-d H:i:s')
) );
}
// Here we update the email field when a record is edited
function updateUserRecords($db, $id, $row) {
$db->edit('adminGroups', array(
'email' => $row['email'],
'updated' => date('Y-m-d H:i:s')
) );
$db->edit('docViewGroups', array(
'email' => $row['email'],
'updated' => date('Y-m-d H:i:s')
) );
$db->edit('preferences', array(
'email' => $row['email'],
'updated' => date('Y-m-d H:i:s')
) );
}
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'roles', 'id' )
->fields(
Field::inst( 'email' )
->validator( Validate::notEmpty() )
->validator( Validate::minLen( 2 ) ),
Field::inst( 'active' )
->validator( Validate::notEmpty() ),
// ...Omitted rows of fields here...
Field::inst( 'updated' )
->setValue(date("Y-m-d H:i:s"))
->getFormatter( function( $val, $data, $opts) {
return date("Y-m-d H:i:s", strtotime( $val) );
} )
->setFormatter( function( $val, $data, $opts) {
return date("Y-m-d H:i:s", strtotime($val));
} )
)
->on( 'postCreate', function( $editor, $id, $values, $row ) {
// Now create a new record in adminGroups, docViewGroups, and preferences
insertNewUserRecords($editor->db(), $values);
} )
->on( 'postEdit', function( $editor, $id, $values, $row ) {
// Now update the corresponding records in adminGroups, docViewGroups, and preferences
updateUserRecords($editor->db(), $id, $values);
} )
->process( $_POST )
->json();
I have a postEdit handler that wants to update the email field in the other three tables if the user edits that field in the roles table. This is where the problems arise.
First of all, the call to updateUserRecords in the postEdit handler fails, getting Fatal error: Uncaught Error: Call to undefined method DataTables\Database::edit() in the function declaration(line 19). I don't see why this should happen, as the insert method is not undefined in the prior function and the structures appear to be parallel.
Second, I'm not sure how I should be using $id **there, as the example in the documentation does not show an example of an **edit call.
Third, $id is not valid in the other tables, as they have been constructed differently over time, and a person might not have the same id in each of the other tables. However, in the other tables, the email field is unique, and a key (but not primary), and I'd like to be able to update the other tables based on the email field. I don't see how to do that.
Can you provide some guidance here, please?
Thanks,
Tom