insertId() on postCreate
insertId() on postCreate
vincmeister
Posts: 136Questions: 36Answers: 4
in Editor
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:
This discussion has been closed.
Answers
Hi Danny,
Store the result of
->exec()
into a variable. That will be the result object and you can useinsertId()
on that.Allan
Hi Allan,
Any example please? I'm stuck.
Btw, the id from users table is auto increment
Danny
Allan
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
What do you mean the "next postCreate"?
on my code above, i have 2 event postCreate
I want to get the last insertId, which is done from your example above.
I want to pass the $user_id to the 2nd postCreate. This one is not working, Undefined $user_id
Please advise, thank you
Oh I see - I hadn't quite realised that - sorry.
Three options:
$GLOBALS
(basically the same as option 2).Allan
Hi Allan,
Many thanks for the help, the 1st option is working for me
Danny