postDelete function cannot get working

postDelete function cannot get working

cpshartcpshart Posts: 246Questions: 49Answers: 5

Hi

I have set-up parent child datatables as per your blog post https://datatables.net/blog/2019-01-11, for cash accounts. I have successfully set-up
->on( 'postCreate' and ->on( 'postEdit' functions which successfully update the parent table balance value (being summation of child amount values) on a Create and/or Edit of a child row.

Extract of my server file

->on( 'postCreate', function ( $editor, $id, $values, $row ) {
    $amount = $row['dm_cash_accountlines']['amount'];

    $stmt = ('SELECT SUM(
    IF (type="CREDIT",amount,-amount)
    ) AS amount_sum
                   FROM dm_cash_accountlines   
                  WHERE user_id  = :userid
                  AND cash_accounts_id = :id
                  GROUP BY :id
                  LIMIT 1');
    $result = $editor->db()->raw()
        ->bind( ':amount', $row['dm_cash_accountlines']['amount'] ) 
        ->bind( ':id', $row['dm_cash_accountlines']['cash_accounts_id'] ) 
        ->bind( ':userid', $_POST['userid'] ) 
        ->exec($stmt);
        
    $amtsum = $result->fetch(PDO::FETCH_ASSOC);
    $editor->db()->raw()
       ->bind( ':amount', $amtsum['amount_sum'] )
       ->bind( ':id', $row['dm_cash_accountlines']['cash_accounts_id'] )
       ->exec( 'UPDATE dm_cash_accounts  
                   SET balance = :amount
                 WHERE id = :id' );
})

The postEdit uses the same code as above except for the first line of code
->on( 'postEdit', function ( $editor, $id, $values, $row ) {

I had assumed that the above code would work when applied to the
->on( 'postDelete', function ( $editor, $id, $values, $row ) {

but deleting a child row has no effect on the balance shown in the parent table, whereas adding a New or Editing an existing child row and changing the amount will display sum(amount) in the parent table balance.

I have attached a screen shot.

I can provide access to my system if required to investigate the problem.

The problem is on the following webpage

https://www.dividendview.co.uk/cash-accounts/

server file
/public_html/Editor-PHP-1.9.4/controllers/dview-cash_accountlines.php

client file
accessible from Button Table Snippet at bottom of webpage

Best regards

Colin

Replies

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    Cannot work like this with delete: no parameter $row because that is the data read back from the database which obviously doesn't exist after you deleted the database record. And it is not called like that ...

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    postRemove might do what you want, but you need to change away from using $row as @rf1234 indicates, since that doesn't exist there. That should hopefully be the only change needed in your code.

    Allan

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Alan and rf1234

    Thanks for the advice, I will investigate using postRemove

    Colin

This discussion has been closed.