We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33983 ☆ A M B ☆
    • 181 Posts
    I have an object that will not insert/create on $object->save() but it will save just fine if I assign an ID so that it is updating rather then inserting. I am trying to debug but I can’t get any error messages.

    Here is my code:
    <?php
        $slideAlbum = $modx->newObject('jgSlideshowAlbum');
        $slideAlbum->fromArray($input_data,'',true);
        
        // debug:
        $modx->setDebug(true);    
        $modx->setLogLevel(LOG_LEVEL_DEBUG);// xPDO_LOG_LEVEL_DEBUG
        
        if ( $slideAlbum->save() ) {
            // then okay
        } else {
            // then failed
            $error = $modx->errorCode();
            $modx->log(modX::LOG_LEVEL_ERROR,'Did not save slideshow_album: '.$error );
            
        }
    ?>
    


    Any thoughts on why it is not working or how to debug this?

    Thanks for read and any help you might have.

    Running on 2.0.8pl on PHP 5.2.17, IIS 7.5
      • 21257 MODX Staff
      • 730 Posts
      Can you post or pastebin your schema?
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 33983 ☆ A M B ☆
        • 181 Posts
        Here is the xml file
          • 21257 MODX Staff
          • 730 Posts
          I think you should omit $setPrimaryKeys from ->fromArray or set it to false:

          $slideAlbum->fromArray($input_data,'');
          

            Mike Schell
            Lead Developer, MODX Cloud
            Email: [email protected]
            GitHub: https://github.com/netProphET/
            Twitter: @mkschell
            • 33983 ☆ A M B ☆
            • 181 Posts
            Thanks, but it did not effect the save().
              • 21257 MODX Staff
              • 730 Posts
              Are you looking in the modx error log for messages?
              Reports > Error Log
              (or core/cache/logs/error.log)
                Mike Schell
                Lead Developer, MODX Cloud
                Email: [email protected]
                GitHub: https://github.com/netProphET/
                Twitter: @mkschell
                • 33983 ☆ A M B ☆
                • 181 Posts
                I looked in Reports->Error Log, I did not know to look for a file log.

                Anyway I did find out what my code problem was. I did not state all of the db columns in fromArray(), one that was set to NOT NULL. Once I put that in the fromArray() it worked. But I don’t get why I did not get an error/warning message telling me that I need to add every NOT NULL value in the insert. I have MySQL 5.5.8.

                Thanks for the help.

                  • 22303 MODX Staff
                  • 10,725 Posts
                  In your snippet you can direct the logTarget to ’HTML’ and then set it back, e.g.
                  <?php
                  // redirect log output to echo'd HTML
                  $oldTarget = $modx->setLogTarget('HTML');
                  
                  // your code here
                  
                  // restore the default logging (to file)
                  $modx->setLogTarget($oldTarget);
                  

                    • 24865
                    • 289 Posts
                    A small side note, you can always access your logs via /core/cache/logs/error.log in your browser. It’s a bit more handy then using the textarea within MODx.
                    Also, validating your object before saving is always a good idea. Sometimes it simply returns NULL and using object modifiers will get you into all sorts of trouble. I usually like to validate it as !is_null($object) and is_object($object). smiley
                      @MarkGHErnst

                      Developer at Adwise Internetmarketing, the Netherlands.