$page = $modx->newObject('modResource');
// add some stuff, do some stuff...
$page->save();
$insert = $modx->query('SELECT LAST_INSERT_ID()');
$this_insert = $insert->fetch(PDO::FETCH_NUM);
print 'Page ' . $this_insert[0].' created.<br/>';
$page = $modx->newObject('modResource');
// add some stuff, do some stuff...
$page->save();
$modx->reloadContext('web'); // or whatever the doc's context is
$doc = $modx->getObject('modResource', array('alias' => 'whatever'));
if ($doc) {
$docId = $doc->get('id');
}
$page = $modx->newObject('modResource');
// add some stuff, do some stuff...
$page->save();
print 'Page ' . $page->get('id') . ' created.<br/>';
Josh, you usually have to call reloadContext() first if it's a newly created doc. Otherwise, MODX will look in the document map and not find it (unless things have changed).
If everything saves correctly, xpdo should already be calling lastInsertId and loading it into the model. So just calling get for id should work as well.Hi Josh,
$dataobject->save();
$dataobject->get('id');