insertId() on postCreate

insertId() on postCreate

vincmeistervincmeister Posts: 136Questions: 36Answers: 4

Hi Allan,

I have 3 tables, employees, users and user_permission
I want to create users automatically from employee create and set the user_permission using on postCreate

Editor : Employee, postCreate users, postCreate user_permission

$editor->on('postCreate',function( $editor, $id, $values, $row ) {
        $vericode = rand(100000,999999);
        $fname = strtolower($values['employees']['first_name']);
        $lname = strtolower($values['employees']['last_name']);
        if ($lname == ''){
            $lname = $fname;
        } 
        $username = $fname . '.' . $lname;
        $join_date = $values['employees']['join_date'];
        $join_date = date('Y-m-d',strtotime($join_date));
        $editor->db()
            ->query('insert', 'users')
            ->set('username',$username)
            ->set('password',password_hash('password', PASSWORD_BCRYPT, array('cost' => 12)))
            ->set('permissions',1)
            ->set('account_owner',1)
            ->set('company','none')
            ->set('fname',$values['employees']['first_name'])
            ->set('lname',$values['employees']['last_name'])
            ->set('join_date',$join_date)
            ->set('email_verified',1)
            ->set('active',1)
            ->set('vericode',$vericode)
            ->exec();
        //$user_id = $editor->insertId(); <- not working
//
        });
    $editor->on('postCreate',function( $editor, $id, $values, $row ) {
        $editor->db()
            ->query('insert', 'user_permission_matches')
            ->set('user_id',$user_id)
            ->set('permission_id',1)
            ->exec();
    });

My question: How to get the last inserted id from users table, and pass it to user_permission table
I got error Call to undefined method DataTables\Editor::insertId()
Please advise, Thank you

BR
Danny

This question has accepted answers - jump to:

Answers

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

    Hi Danny,

    Store the result of ->exec() into a variable. That will be the result object and you can use insertId() on that.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Hi Allan,
    Any example please? I'm stuck.
    Btw, the id from users table is auto increment

    Danny

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓
    $myVariable = $editor->db()
      ...
      ->exec();
    
    $id = $myVariable->insertId();
    

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    HI Allan,

    Thanks for the example. It works, but i need to pass the $id into next postCreate. I got undefined index. Please advise, thank you

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

    What do you mean the "next postCreate"?

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    on my code above, i have 2 event postCreate
    I want to get the last insertId, which is done from your example above.

    ->on('postCreate',function( $editor, $id, $values, $row ) {
            $vericode = rand(100000,999999);
            $fname = strtolower($values['hrm_employees']['first_name']);
            $lname = strtolower($values['hrm_employees']['last_name']);
            if ($lname == ''){
                $lname = $fname;
            } 
            $username = $fname . '.' . $lname;
            $join_date = $values['hrm_employees']['join_date'];
            $join_date = date('Y-m-d',strtotime($join_date));
            $edt = $editor->db()
                ->query('insert', 'users')
                ->set('username',$username)
                ->set('password',password_hash('password', PASSWORD_BCRYPT, array('cost' => 12)))
                ->set('permissions',1)
                ->set('account_owner',1)
                ->set('company','none')
                ->set('fname',$values['hrm_employees']['first_name'])
                ->set('lname',$values['hrm_employees']['last_name'])
                ->set('join_date',$join_date)
                ->set('email_verified',1)
                ->set('active',1)
                ->set('vericode',$vericode)
                ->set('custom1',$values['hrm_employees']['nik'])
                ->exec();
                $user_id = $edt->insertId(); // this is works, i want to pass to code below
            });
        //2nd postCreate
    ->on('postCreate',function( $editor, $id, $values, $row ) {
            $user_id = $edt->insertId();
            $editor->db()
                ->query('insert', 'user_permission_matches')
                ->set('user_id',$user_id)
                ->set('permission_id',1)
                ->exec();
        })
    

    I want to pass the $user_id to the 2nd postCreate. This one is not working, Undefined $user_id

    Please advise, thank you

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

    Oh I see - I hadn't quite realised that - sorry.

    Three options:

    1. Merge the two functions together
    2. Assign the value to a global variable which will be usable by both
    3. Assign the value to a property in $GLOBALS (basically the same as option 2).

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Hi Allan,

    Many thanks for the help, the 1st option is working for me

    Danny

This discussion has been closed.