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

    i'm trying to get my first quiz working for wqhich i made a custom table. to cut a long story short.
    im trying to create a Quizuser by saving only the sessionID to the DB and than later the user can fillout his email adres. this all works fine

     $fields = array(
            'sessionid' => session_id()
        );
    
     $user = $modx->newObject('VcUser', $fields);
        if ( $user->save()) {
            $output .= "ga naar vraag 1";
        } else {
            $output .= "Error";
        }


    Does this save() return some error when i do a save and the sessionID allready exist (its unique )? what kind of error can i expect ?
      • 26903
      • 1,336 Posts
      If I remember rightly and unless things have changed since I looked at this last you can't get at the specific error code that caused the failure, you need to query the embedded PDO object to get its last error but you can't get at it through XPDO. I may have raised a ticket on this, its definitely on the forums somewhere.
        Use MODx, or the cat gets it!
        • 24629
        • 370 Posts
        Hi,

        not being an hardcore developer i'm having a hard time even formulating my questions clearly smiley
        How would i go about querying my embedded PDO object?

        tnx
          • 26903
          • 1,336 Posts
          I think this was the problem, you couldn't get at it using the XPDO Api, so you couldn't query it for its last error and XPDO doesn't make this available either.
            Use MODx, or the cat gets it!
            • 22303 MODX Staff
            • 10,725 Posts
            The only way to get this currently is by switching the ERRMODE of the underlying PDO object, i.e.
            $fields = array(
               'sessionid' => session_id()
            );
            $user = $modx->newObject('VcUser', $fields);
            
            $modx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            try {
                $user->save();
                $output .= "ga naar vraag 1"
            } catch (PDOException $pe) {
                $output .= $pe->getMessage();
            }
            $modx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
            

            Don't forget to set it back when done to prevent exceptions from disrupting core operation.