Why php editor preCreate not working

Why php editor preCreate not working

fiofiottefiofiotte Posts: 9Questions: 3Answers: 1

Hi,

I'm wondering why the preCreate function in Editor part of my php file isn't working !!

I'm using Datatables 1.10 with Editor and server-side process.

After building my editor instance, when I'm trying to make :

->on( 'preCreate', function($editor, $values) {
        $editor->db()
            ->query('update','table')
            ->set('order_id','uid', false)
            ->where('order_id','0', "=")
            ->exec();
    })

the row is correctly created on both side (database AND datatable) but the order_id field is still 0.

I tried doing the same thing with postCreate function and it's quite working !
The order_id in the database is equal to the uid as expected but now the event occurs after row creation so the row is on top of the datatable and not on the bottom as it should be. I need to redraw the table to see it where I expected it.

If someone has an idea on how to explain this, it would be very apreciated. Thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    Have you got the preCreate event before the process() method call? Also, are you using the 1.5.x libraries for Editor (the events were introduced in 1.5.0 - 1.5.4 is the current release at the time of writing).

    Allan

  • fiofiottefiofiotte Posts: 9Questions: 3Answers: 1

    Hi Allan,

    I'm working with the last version : 1.5.4

    I have the preCreate before the process() method :

    Editor::inst( $db, $table )
        ->fields(
            Field::inst( 'order_id' ),
            Field::inst( 'cat' ),
            Field::inst( 'ss_cat' ),
            Field::inst( 'created_at' ),
            Field::inst( 'updated_at' )
        )
        ->on( 'postCreate', function($editor, $values) {
            $editor->db()
                ->query('update','table')
                ->set('order_id','uid', false)
                ->where('order_id','0', "=")
                ->exec();
        })
        ->where('cat', $categorie)
        ->process( $_POST )
        ->json();
    

    I have tried putting it after and the row creation is done but still 0 in order_id on database.

    Just to clear things up, what I'm trying to achieve is setting the order_id at the last value of my uid in the database. I'm using uid in my database as the primary key, so I would have set the order_id equals to the uid just after the creation on database but before adding it to datatable (for row ordering).

    Maybe it's because the row cannot be created with a value that isn't existing already !! But preCreate : Pre-row creation event - triggered before the row is added to the DataTable so the row should have already been created in the database !!

    I tried some workarounds like getting the last existing value of uid or order_id in the database and just add +1 to it but it's not working (maybe because of a wrong php code (I'm new with PDO statements))

    ... it's driving me crazy ... :/

    Thanks

    C.

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin
    edited January 2016 Answer ✓

    The documentation in preCreate is not relevant at all here - that is the Javascript event on the client-side. Ignore that completely - you are using a server-side event here (which happens to share the same name).

    The documentation you want is in the Editor PHP manual. I think the issue specifically here is that the parameters you are using for postCreate are not correct. postCreate is passed in four parameters where the second one is the row id that has just been created, which I presume is what you want here.

    Allan

  • fiofiottefiofiotte Posts: 9Questions: 3Answers: 1
    edited January 2016

    Ahhh !!! :D

    I understand much much much better.

    Now, I do the mysql process on php postCreate :

    ->on( 'postCreate', function($editor, $id, $values, $row) {
            $editor->db()
                ->query('update','table')
                ->set('order_id',$id, false)
                ->where('order_id','0', "=")
                ->exec();
        })
    

    and on the javascript client-side, I reload the table on postCreate :

    .on( 'postCreate postRemove', function (e, json) {
                table.ajax.reload( null, false );
            } );
    

    The order_id has the uid value and the data is set at the end of the table.

    Thanks for the link to the documentation I didn't saw. And many thanks for your reactivity !!

    C.

This discussion has been closed.