posCreate with auto increment column

posCreate with auto increment column

Khalid TeliKhalid Teli Posts: 251Questions: 71Answers: 0

I am using postCreate function to to send data to another table, for example

 ->on( 'postCreate', function ( $editor, $id, $values ) use ( $db ) {
         $db->raw()

  ->bind( ':e',     $values["product_code_fk"] )
    ->bind( ':f',     $values["contract_id"] )

  ->exec( 'INSERT INTO prodycts_balance (product_code_fk, contract_id_fk) 
   select :e, :f   FROM contracts Where product_code_fk = :e');

    })

The issue here is that contract_idis a primary key with auto incrementvalues . so as expected it is showing the error contract_id not found. It is because we are not giving the values during create .

Is there a war around for this, for example these values are read from database after they are created and then sent to different table using postCreate?

Thank you

Answers

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Hi,

    In postCreate (are the server-side) you have a fourth parameter as noted in the docs. That will give you the newly created primary key value. The values array is what was submitted. The forth parameter ($row) is what was read back from the database.

    Allan

  • Khalid TeliKhalid Teli Posts: 251Questions: 71Answers: 0
    edited November 2020

    Hi @allan
    Thank you for the reply. The issue here is , I use two post create
    For each created row, it creates 12 rows in same table based on the insert query below
    1)

            ->on( 'postCreate', function ( $editor, $id, $values ) use ( $db ) {
                 $db->raw()
        
          ->bind( ':a',     $values["product_code_fk"] )
           ->bind( ':b',      $values["contract_prod"] )
          ->bind( ':c',          $values["start_date"] )
            ->bind( ':d',          $values["end_date"] )
                ->bind( ':e',          $values["list_supp"] )
        
        
          ->exec( 'INSERT INTO crg_contract_forms (product_code_fk,contract_prod,member_name,start_date,end_date, list_supp) 
           select :a , :b, crg_members.member_name,:c, 
           :d, list_supp  FROM crg_contract_forms JOIN crg_members Where product_code_fk = :a AND start_date = :c AND list_supp = :e ; DELETE 
        FROM crg_contract_forms
        WHERE member_name = ""');
            })
    

    and after this , I want to create a different table using

    2)

        ->on( 'postCreate', function ( $editor, $id, $values, $row ) use ( $db ) {
                 $db->raw()
        
          ->bind( ':f',     $values["product_code_fk"] )
            ->bind( ':g',      $row["contract_id"] )
          
        
        
        
          ->exec( 'INSERT INTO crg_directory_contracts_balance (product_code_fk, contract_id_fk) 
           select :f, :g FROM crg_contract_forms ');
            })
    

    The isuue is that $row["contract_id"] ) takes id of first row created. how can I include the auto increment id's of all rows create dusing first postCreate?

    Thnak you

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    $res = $db->raw('...');
    $id = $res->insertId();
    

    If that doesn't work, then you might want to just use the PDO connection directly which is $db->resource();.

    Allan

This discussion has been closed.