2

I am trying to get the ID of a last row I've inserted with a php/mysql query:

$createjob = $modx->query($createjob);

$lastId = $modx->lastInsertId();

but this does not seem to be working.

Does any one know the correct way of doing this with ModX PDO?

MeltingDog
  • 14,310
  • 43
  • 165
  • 295

2 Answers2

1

Try this:

$createJob = $modx->newObject('CreateJob');
$createJob->set( 'value', 1234 );

// try saving
if( $createJob->save() ){
    echo $modx->lastInsertId();
}

Read more here

anp
  • 537
  • 3
  • 16
  • This example has some overhead and limitations. Save operation takes resources cause we are saving an object and this object can have related objects (xpdo tries to save them when you want to save an object). Also sometimes i just want to get max id and not to save an object. – rogaldh Dec 26 '14 at 10:27
0

Try simple sql query which gives the max id for record with specified class.

$q = $modx->newQuery('modResource');
$q->select(array(
   "max(id)",    
)); 

$id = $modx->getValue($q->prepare());
rogaldh
  • 268
  • 1
  • 9