We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14372
    • 49 Posts
    What are the conditions or the possible reasons as to why an xPDO save would fail?

    Please note: That, the package was created successfully and the object as well ...

    As per the code below ...

    <?php
    
    /* Add ah_postings package and throw an error if unsuccessful */
    $path = MODX_CORE_PATH . 'components/ah_postings/';
    $result = $modx->addPackage('ah_postings',$path . 'model/','vw_ah_pos_');
    
    if (! $result) {
      $errorMsg='Failed to add package ';
      $hook->addError('error_message',$errorMsg); 
      return false;
    }
    
    /* Create new posting object & check if object was created or else throw an error */
    $position = $modx -> newObject('Postings');
    
    if (!is_object($position) || !($position instanceof xPDOObject)) {
        $errorMsg='Failed to create object of type:Postings ';
        $hook->addError('error_message',$errorMsg); 
        return false;
    }
    
    /* Set data from form */
    
    $position->set('usr', $_POST['usr']);
    
    /* Save data from form and throw out errors if fails */
    
    if (!$position->save()) {
        $errorMsg='Failed to save data.';
        $hook->addError('error_message',$errorMsg); 
        return false;
    }
    else
        return true;


    I was trying to save one field ... there is a usr field in the table and a placeholder [[+fi.usr]] from which I want to get the data (FormIt Snippet) so the code above is to be used as a formit snippet hook
      • 4172
      • 5,888 Posts
      I think your issue is your id-field. It seems not to be autoincrement.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 14372
        • 49 Posts
        I think so too ... so I’ve tried a couple of things ... but still the save fails.

        What ideas do you have for the id field?

        My SQL Export is as such ... (the table has no data) so I figured this is a good way to show you the structure ...

        CREATE TABLE `vw_ah_pos_postings` (
          `id` int(10) unsigned NOT NULL auto_increment,
          `usr` int(10) unsigned NOT NULL,
          `department` varchar(255) character set latin1 NOT NULL,
          `duration` int(10) unsigned NOT NULL,
          `renumeration` int(1) unsigned NOT NULL,
          `apply-method` int(1) unsigned NOT NULL,
          `interns` int(100) NOT NULL,
          `date` varchar(10) character set latin1 NOT NULL,
          PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


        Please note, that at one point I noticed that I hadn’t put the auto_increment on the primary field. As soon as i did so it managed to save but all the fields were zero. which makes sense because I was only setting one field.

        But now, its broken again ... it isn’t saving ...

        I used Bobray’s example as a guide here; http://bobsguides.com/custom-db-tables.html He creates a very simple DB and I’ve just checked his id field and my id field and they are pretty much the same.
          • 22303 MODX Staff
          • 10,725 Posts
          All of your fields are NOT NULL — unless you set every field with a value, it will not b allowed to save with the NULL defaults you have described in your model.
            • 14372
            • 49 Posts
            I get you .. so I corrected and tried again ... my DB now looks like this ...

            CREATE TABLE `vw_ah_pos_postings` (
              `id` int(10) unsigned NOT NULL auto_increment,
              `usr` int(10) unsigned default NULL,
              `department` varchar(255) character set latin1 default NULL,
              `duration` int(10) unsigned default NULL,
              `renumeration` int(1) unsigned default NULL,
              `apply-method` int(1) unsigned default NULL,
              `interns` int(100) unsigned default NULL,
              `date` varchar(10) character set latin1 default NULL,
              PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


            FYI ... I recreated the classes and maps (deleted the whole package and ran the script again) ... also tested the script by checking whether the package is successfully created e.t.c. ...

            Also ... just to put this problem in context ... this is my hook (to FormIt snippet) that’s meant to copy the data to DB before calling the email hook ...


            <?php
            
            /* Get formit data and store in variables */
            $db_usr = $hook->getValue('usr');
            $db_department = $hook->getValue('department');
            $db_duration = $hook->getValue('duration');
            $db_renumeration = $hook->getValue('renumeration');
            $db_applymethod = $hook->getValue('apply-method');
            $db_interns = $hook->getValue('interns');
            $db_date = $hook->getValue('date');
            
            /* Add ah_postings package and throw an error if unsuccessful */
            $path = MODX_CORE_PATH . 'components/ah_postings/';
            $result = $modx->addPackage('ah_postings',$path . 'model/','vw_ah_pos_');
            
            if (! $result) {
              $errorMsg='Failed to add package ';
              $hook->addError('error_message',$errorMsg); 
              return false;
            }
            
            /* Create new posting object & check if object was created or else throw an error */
            $position = $modx -> newObject('Postings');
            
            if (!is_object($position) || !($position instanceof xPDOObject)) {
                $errorMsg='Failed to create object of type:Postings ';
                $hook->addError('error_message',$errorMsg); 
                return false;
            }
            
            /* Set data from form */
            
            $position -> set('usr', $db_usr);
            $position -> set('department', $db_department);
            $position -> set('duration', $db_duration);
            $position -> set('renumeration', $db_renumeration);
            $position -> set('apply-method', $db_applymethod);
            $position -> set('interns', $db_interns);
            $position -> set('date', $db_date);
            
            /* Save data from form and throw out errors if fails */
            
            if (!$position->save()) {
                $errorMsg='Failed to save data.';
                $hook->addError('error_message',$errorMsg); 
                return false;
            }
            else
              return true;
            


            PS: I get the failed to save error always.

            Something tells me that there is something really small that I’m doing wrong and I’m posting all this in the forums in the hope that someone will tell me "look, you noob ... you left this out!!! smiley"

            Nway, been on this for the past two days :-( So somebody, anybody ... idea are welcome ... keep em coming.
              • 22303 MODX Staff
              • 10,725 Posts
              Are you not getting any errors in the log? (core/cache/logs/error.log)
                • 24068
                • 4 Posts
                a column named "apply-method"?
                try removing that dash with an underscore "_"

                i know, a dash as cloumnname between `` is fine, but you never know.

                And "core/cache/logs/error.log" would be nice to see.
                And the xml of the table-schema too.
                  • 14372
                  • 49 Posts
                  Hey guys,

                  I have good news smiley smiley smiley Its working!!!

                  I don’t know what fixed it so I’ll just lay out my procedure and the corrections I made. As I said before, I knew it was a small error on my part and that’s why for almost 2days I still had hope that xPDO was the way to go.

                  Anyway,

                  1) I made the fields to have NULL as a default and also changed the apply-method field to application. (Both dumb decisions)
                  2) Deleted the package from core/components
                  3) Used BobRay’s CreateXpdoClasses Snippet to recreate the xpdo classes and maps
                  4) Changed necessary placeholders in snippet and form to reflect the DB changes.
                  5) Tested the form ... it gave no error. And sent the mail.
                  6) Checked the DB ... VWALA! The data is there ...

                  NB:// LESSON OF THE DAY

                  i) When things go wrong don’t give up.
                  ii) On the forums, there are really cool people willing to help you out ... don’t be afraid to ask.
                  iii) When things go wrong and you struggle to figure it out, JUST READ, READ READ and TEST TEST TEST.

                  I’ve learnt more about xPDO in the past two days than any other day that I just skimmed the docs to know what its about. Part of me is glad it didn’t work right away coz I got to rewrite Bruno17’s formit2db snippet thinking that its the one that had a problem. I can now write my own snippet, customize it and hopefully improve it. smiley

                  Thanks OpenGeek for xPDO smiley
                  Thanks Bruno17 for formit2db and replying in every thread that I posted this problem.
                  Thanks pureppl for your suggestion as well ...

                  Question: How do you mark a thread as solved? Can’t find it anywhere.
                    • 1169
                    • 312 Posts
                    One I can answer.
                    To mark a Topic as [SOLVED].
                    Go to the first Post in the Topic if you are the topic creator and Edit Post Adding or inserting [SOLVED]
                    And Post.
                      DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31