We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28367
    • 4 Posts
    Hi guys,

    Just can’t seem to get xPDO lastInsertId() method working.

    $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->lastInsertId() . "<br />";
    


    returns an error:

    Fatal error: Call to undefined method RecnikNeparsirani_mysql::lastInsertId() in /home/srpskije/public_html/recnik/core/cache/elements/modsnippet/1.include.cache.php on line 105
    


    I can see the method in docs, but no explanation. And I’ve gotten under impression that lastInsertId was removed from xPDO 2.0, while it is present in 1.0

    Btw, I’m using Revo.

    Thanks a bunch. Really need this to work for me.

      • 28215
      • 4,149 Posts
      splittingred Reply #2, 16 years ago
      Try $modx->lastInsertId();
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 28367
        • 4 Posts
        it works, thanks!
          • 22303 MODX Staff
          • 10,725 Posts
          But you don’t need to call this method. Just use the object which already has the primary key field updated from the lastInsertId() method...

          $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->getPrimaryKey() . "<br />";
            • 28367
            • 4 Posts
            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?
              • 6228
              • 249 Posts
              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
                • 26903
                • 1,336 Posts
                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!
                  • 22303 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.
                    • 26568
                    • 30 Posts
                    I think its good point to add into documentation, for example here
                    http://rtfm.modx.com/display/xPDO20/save
                      • 14372
                      • 49 Posts
                      Could someone provide links to the docs that include lastInsertId(); and getPrimaryKey();

                      And also probably add it to http://rtfm.modx.com/display/xPDO20/save


                      Actually a lot of commands here (link below) aren't in the MODx Docs

                      Summary of Legacy Code Removed in 2.1
                      http://rtfm.modx.com/display/revolution20/Summary+of+Legacy+Code+Removed+in+2.1

                      MODx Class Reference
                      http://rtfm.modx.com/display/revolution20/Class+Reference [ed. note: jobkingori last edited this post 14 years, 3 months ago.]