We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40122
    • 330 Posts
    I am trying to get the last ID of a an SQL row just inserted.

    Normally I would do this as such:

    $modx->query($query);
    
    $lastId = $modx->lastInsertId();


    But I cannot get this to work.

    Would anyone know what I should be doing?

    Thanks!
      • 4172
      • 5,888 Posts
      If you would use xpdo-objects and methods, this should work:

      if ($object = $modx->newObject($classname)){
          $object->fromArray($values);
          if ($object->save()){
              $lastId = $object->get('id');
          }
      }
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 24865
        • 289 Posts
        I think your main focus should be figuring out why your lastInsertId isn't working, as it works with PDO::lastInsertId(). Is your table constructed out of multiple, custom primary keys, or is your table a simple xPDOSimpleObject?
          @MarkGHErnst

          Developer at Adwise Internetmarketing, the Netherlands.