Child created/updated upon creation/update of the parent

Child created/updated upon creation/update of the parent

carrarachristophecarrarachristophe Posts: 109Questions: 25Answers: 2
edited November 16 in Editor

Hello,

I have 2 tables:
Transaction
*id
*date
*amount
*flowid

Flow
*id
*date
*amount

My datatables works fine for the 1st one.
But what I am trying to do is:
1. when creating the transaction, it creates as well the flow at the same time
2. when updating the transaction, it updates the flow (example: amount or date, based on the amount or date of the transaction) at the same time

It is not exactly a parent child editor because I want the flow to be created automatically.

Did anyone manage to implement something similar?

Replies

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421
    edited November 17

    Add this to your php Editor for "transaction"

    Editor::inst( $db, 'transaction' )
    ....
    ->on( 'writeCreate', function ( $editor, $id, $values ) use ( $db ) {
        $db->insert( 'flow', array(
            'transaction_id' => $id,
            'date'           => $values['yourDate'],
            'amount'         => $values['yourAmount']
        ) );
    } )
    ->on( 'writeEdit', function ( $editor, $id, $values ) use ( $db ) {
         $db->update( 'flow', array( 
             'date'        => $values['yourDate'],
             'amount'      => $values['yourAmount']
         ), array( 'transaction_id' => $id) );
    } )
    

    when updating the transaction, it updates the flow (example: amount or date, based on the amount or date of the transaction) at the same time

    I would not recommend to do the UPDATE without having a foreign key of "transaction" in "flow" because the update might go wrong if you only do the matching based on the previous amount and date of the transaction. Hence my code requires the implementation of "transaction_id" as foreign key in table "flow".

    Flow
    *id
    *transaction_id
    *date
    *amount

    If you are not reading back the changed "flow" entries with this Editor, you can also use "postCreate" and "postEdit". Then you can also use "$row" to use the values read back from table "transaction".

    You can also use your own db-handler in the above events with "global".

  • carrarachristophecarrarachristophe Posts: 109Questions: 25Answers: 2

    Thank you rf1234.
    As a matter of fact, although I did not mention it, I had a transaction_id equivalent field.
    I followed your code but am getting the following message: Undefined array key "date".
    I will look into it and keek you posted

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421

    If you want more flexibility, you can also use Editor's "raw" method with real SQL like this

    $db->raw()
       ->bind( ':transaction_id', $id ) 
       ->bind( ':date', $values['yourDate'] )
       ->bind( ':amount', $values['yourAmount'] )
       ->exec( 'UPDATE flow
                   SET `date`          = :date,
                       `amount`        = :amount
                 WHERE transaction_id  = :transaction_id' );
    
Sign In or Register to comment.