parent child update parent value after deleting child record

parent child update parent value after deleting child record

cpshartcpshart Posts: 246Questions: 49Answers: 5
edited November 2021 in Free community support

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Hi

I have created a parent child set-up for stock holdings as shown below

If I Create or Edit the child rows Quantity, the Parent Quantity value is updated to the SUM Of Child Quantity

This is controlled by the server code below

->on( 'postEdit', function ( $editor, $id, $values, $row ) {
    $quantity = $row['dm_holdinglines']['quantity'];

    $stmt = ('SELECT SUM(
    IF(transaction_type="Buy",quantity,-quantity)
    ) AS quantity_sum
                   FROM dm_holdinglines   
                  WHERE user_id  = :userid
                  AND portfolio_id = :portfolio
                  AND stock_id = :stock
                  GROUP BY :portfolio, :stock
                  LIMIT 1');
    $result = $editor->db()->raw()
        ->bind( ':transaction_type', $row['dm_holdinglines']['transaction_type'] ) 
        ->bind( ':quantity', $row['dm_holdinglines']['quantity'] ) 
        ->bind( ':userid', $_POST['userid'] ) 
        ->bind( ':portfolio', $row['dm_holdinglines']['portfolio_id'] ) 
        ->bind( ':stock', $row['dm_holdinglines']['stock_id'] ) 
        ->exec($stmt);
        
    $qtysum = $result->fetch(PDO::FETCH_ASSOC);
    $editor->db()->raw()
       ->bind( ':quantity', $qtysum['quantity_sum'] )
       ->bind( ':id', $row['dm_holdinglines']['holdings_id'] )
       ->exec( 'UPDATE dm_holdings  
                   SET quantity = :quantity
                 WHERE id = :id' );
})

and an identical code snippet as above for

->on( 'postCreate', function ( $editor, $id, $values, $row ) { ... remaining block as above

Not my main question but can I join the two above snippets of the form or similar ??
```
->on( 'postCreate postEdit', function ( $editor, $id, $values, $row ) { ... remaining block as above

My main question is I need to be able to delete a child row

using postRemove or similar SUM Quantity child rows after deletion (in the same way as shown above for postCreate and postEdit)

UPDATE the Parent Quantity as shown above.

However the $row parameter is not available in the postRemove function, is there an alternative way.

I can provide access to my system if required.

Many Thanks

Colin

This question has an accepted answers - jump to answer

Answers

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Sorry, this question was asked here and a response given

    https://datatables.net/forums/discussion/65047/postdelete-function-cannot-get-working#latest

    Thanks Colin

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Answer ✓

    Hi Colin,

    Just to confirm - are you happy with the replies in the other thread? Or is this still something that is ongoing?

    Allan

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Allan

    The deletion of a child row updating parent Quantity is now working by adding the same code block as shown above for the postRemove in the server script and also changing all references to $row to $values

    ->on( 'postRemove', function ( $editor, $id, $values ) {

    Many Thanks

    Colin

Sign In or Register to comment.