Try $modx->lastInsertId();
shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect |
github |
splittingred.com
This is more logical in the sense of using the object that I already use. However getPrimaryKey doesn’t sound logical.
Is this more efficient than calling $modx->lastInsertId?
I’ve been using this syntax, assuming ’id’ is the primary key. Are there caveats to this approach?:
$neparsirani = $modx->newObject('RecnikNeparsirani');
$neparsirani->fromArray(array(
'na_slovo' => 'A',
'red_mw' => $red['red_mw'],
'red_db' => $red['red_db'],
'tip_reda' => $red['tipReda']
));
$neparsirani->save();
echo "last insert id: ". $neparsirani->get('id') . "<br />";
lo9on.com
MODx Evolution/Revolution | Remote Desktop Training | Development
There’s nothing to stop MYSQL creating the keys out of sequence, so the last id will above will be the id of the last object created in time but not necessarily the id with the highest numbered auto_increment column.
Use MODx, or the cat gets it!
-
MODX Staff
- 10,725 Posts
There is no caveat between using get(’id’) and getPrimaryKey(); the latter is just a generic shortcut to get the value of whatever is defined as the primary key. Note this could also be an array of fields/values.