Is row actually created before PHP Event ->on('postCreate'... ) is fired?

Is row actually created before PHP Event ->on('postCreate'... ) is fired?

rw152rw152 Posts: 56Questions: 15Answers: 1

I've noticed when using ...on('postCreate'...) in editor that the row doesn't actually seem to be created prior to this functions calling. In this 'postCreate' event, I try to use the $id parameter to get the id of the newly created row so I can generate a PDF from it... However, the row doesn't seem to exist when I call it.

It's odd, though, because it does return an appropriate $id integer, it's just that the row isn't actually created.

Rows are generated correctly if I don't call that PDF function.

This question has an accepted answers - jump to answer

Answers

  • rw152rw152 Posts: 56Questions: 15Answers: 1

    Also, what's interesting, is the $id increments correctly if i put a die($id); statement in the code, but still no records are inserted into the DB. so, for example:

    ->on( "postCreate", function ( $editor, $id, $values, $row ) {
       $rec = $this->SendEmailRes(intVal($id));                 
       die("id " . $id);
    });
    

    $id will increase by 1 each time, but no rows are actually inserted into the DB (presumably because of the die() )

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Hi,

    It is created, but it is created in a transaction which might explain why you are seeing the effect you are. Only when all rows have been edited (to allow for multi-row editing) is the transaction committed. This is to allow a rollback should something go wrong!

    There are a couple of options I can think of to workaround this:

    1. Disable the transaction behaviour of Editor using the Editor->transaction() method
    2. Use $editor->db() to get the database instance Editor is using (which is in the transaction) and use that for your DB queries.

    1) is probably preferable in this case if you already have your code doing the database interaction required.

    Allan

  • rw152rw152 Posts: 56Questions: 15Answers: 1

    Thanks, Allan!

    I ended up just sending the email after the Editor->process().... event. Thanks for giving me that detailed synopsis. I figured it was a transaction issue, and it's great to know that failsafe exists!

This discussion has been closed.