writeCreate/postCreate issue

writeCreate/postCreate issue

tom@pdptom@pdp Posts: 19Questions: 7Answers: 0

Hi,

For some reason

$editor->on('writeCreate',function( $editor, $id, $values ) use ($pdo) {
            print_r($pdo->query('SELECT * FROM `table` WHERE id=:id',array('id'=>$id))->results());
});

or

$editor->on('postCreate',function( $editor, $id, $values, $row ) use ($pdo) {
            print_r($pdo->query('SELECT * FROM `table` WHERE id=:id',array('id'=>$id))->results());
});

prints me an empty array.

I would expect to get the data instead. $id, $values, $row already exist, but the data is not yet physically written into the DB. I need to run a query with a simple JOIN, which is impossible to execute because the row is still not there. What should I do?

Thanks,

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi Tom,

    Is $pdo a separate PDO instance from the Database instance that the Editor constructor is given (i.e. are you using two PDO instances, or one). I'm guessing two! Editor uses a transaction by default, so if you use a second instance, it will be reading outside the transaction and thus will get the stale data.

    Try:

    $editor->on('postCreate',function( $editor, $id, $values, $row ) {
      $res = $editor->db()->select('table', '*', ['id'=>$id])->fetchAll();
      print_r( $res );
    });
    

    That will use the PDO instance that Editor is using. Database API docs available here.

    Allan

  • tom@pdptom@pdp Posts: 19Questions: 7Answers: 0

    Hello Allan,

    That's what I needed. Thank you. It works.

This discussion has been closed.