We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15051
    • 7 Posts
    Quote from: anso at Sep 22, 2010, 06:57 AM

    Hello

    From

    FormIt Roadmap
    FormIt 2.0.0
    Save form results to DB
    Iterative snippet to grab saved form results and display in table/graph format

    It will happens... but for now you’ll have to write your custom hook/snippets and table schema...
    I think I have seen somewhere in the forum a thread about a formit2db but don’t remember where...
    Cheers

    The example on the forum you are referring to is saving to existing MODx tables using existing MODx xPDO objects e.g. resources. If you need to save to a custom table, you can follow the code I posted above.

    I’ll work on a little tutorial: "How to save formit forms to custom database tables in 5 minutes"

    It’s not difficult but should be documented explicitly somewhere as it’s a common use case.
      • 1381
      • 7 Posts
      Quote from: junkend at Sep 22, 2010, 10:53 AM

      Quote from: anso at Sep 22, 2010, 06:57 AM



      The example on the forum you are referring to is saving to existing MODx tables using existing MODx xPDO objects e.g. resources. If you need to save to a custom table, you can follow the code I posted above.

      I’ll work on a little tutorial: "How to save formit forms to custom database tables in 5 minutes"

      It’s not difficult but should be documented explicitly somewhere as it’s a common use case.

      Waiting for this tutorial. Please make this simple for us noobs smiley
        • 40045
        • 534 Posts
        Can’t wait for this too =), thanks in advance!
          • 21193
          • 9 Posts
          NO news yet?? I can’t wait. I need to save data in a custom table, but I can’t do it in any way.
            • 22019
            • 390 Posts

            It’s really not that hard to do - I learnt it in a few hours and I’m not a developer.

            As BobRay says further up the thread, read the case study on custom tables here
            http://rtfm.modx.com/display/revolution20/Using+Custom+Database+Tables+in+your+3rd+Party+Components
            and then use the snippet posted above in the thread (changing the package name to match the one you’ve created) and call it as a hook in FormIt.

            My FormIt call ends up looking like this:
            [[!FormIt?  &validate=`email:email:required` &emailSubject=`New subscriber` &emailFromName=`YOUR/YOUR CLIENT'S NAME` &emailTo=`YOUR EMAIL ADDRESS`  &hooks=`email,ias_addSubscriber,FormItAutoResponder,redirect` &redirectTo=`5`]]


            ias_addSuscriber is my snippet - which simply repeats what’s above in the thread (comments removed for brevity):
            $base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/ias_newsletter/';
            $modx->addPackage('ias_newsletter',$base_path.'model/');
            $subscriber_form = $modx->newObject('ias_newsletter');
            $subscriber_form->fromArray($scriptProperties['fields']);
            $subscriber_form->save();
            return true;


            The complication, if any, is in creating the package - but seriously, the RTFM doc sorts that out for you (and you need some way of creating the database table anyway, so it might as well be that way - as a model within a package).



            Seriously. A little pain, but you have to create the da
              Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
              • 40045
              • 534 Posts
              The xPDO is the point where I’m stuck, I don’t get that whole package/maps/classes/schema thing...

              there is a snippet here: http://modxcms.com/forums/index.php?topic=54753.0

              called db2formit which acts as a preHook and should create the classes and maps or something like that, it looks like this:

              <?php
              
              $prefix=$scriptProperties['prefix'];
              $packageName = $scriptProperties['packageName'];
              $tablename = $scriptProperties['tablename'];
              
              $restrictPrefix = true;
              
              $packagepath = $modx->getOption('core_path') . 'components/'.$packageName.'/';
              $modelpath = $packagepath.'model/';
              $schemapath = $modelpath.'schema/';
              $schemafile = $schemapath.$packageName.'.mysql.schema.xml';
              
              if (!file_exists($schemafile)){
                  $manager= $modx->getManager();
                  $generator= $manager->getGenerator();
              
                  if (!is_dir($packagepath)) {
                      mkdir($packagepath, 0777);
                  }
                  if (!is_dir($modelpath)) {
                      mkdir($modelpath, 0777);
                  }
                  if (!is_dir($schemapath)) {
                      mkdir($schemapath, 0777);
                  }
                  //Use this to create a schema from an existing database
                  $xml= $generator->writeSchema($schemafile, $packageName, 'xPDOObject', $prefix, $restrictPrefix);
              
                  //Use this to generate classes and maps from your schema
                  // NOTE: by default, only maps are overwritten; delete class files if you want to regenerate classes
                  $generator->parseSchema($schemafile, $modelpath);    
              } 
              
              $modx->addPackage($packageName,$modelpath,$prefix);
              $classname = strtoupper(substr($tablename, 0, 1)) . substr($tablename, 1) . '';
              
              if (isset($_GET['resId'])){
                  if ($dataobject=$modx->getObject($classname,array('id'=>$_GET['resId']))){
                      $hook->setValues($dataobject->toArray());
                  }
              
                  //$errorMsg = '<pre>'.print_r($dataobject->toArray(),true).'</pre>';  
                  //$hook->addError('error_message',$errorMsg);  
              }
              
              
              return true;
              ?>
              


              when I read this, it seems to create the folder structure if it’s not existing and it also makes the schema xml file (this would be the mysql table structure for xpdo when I understand this right...?) but then:

                  //Use this to create a schema from an existing database
                  $xml= $generator->writeSchema($schemafile, $packageName, 'xPDOObject', $prefix, $restrictPrefix);
              
                  //Use this to generate classes and maps from your schema
                  // NOTE: by default, only maps are overwritten; delete class files if you want to regenerate classes
                  $generator->parseSchema($schemafile, $modelpath);   
              


              this $generator class comes into play, what is this and what does it?
                • 3749
                • 24,544 Posts
                The whole idea is to create the classes and maps necessary for xPDO to use your classes (or more precisely, to allow you to use xPDO with your classes.) It only has to be run once. BTW, this doesn’t necessarily have anything to do with FormIt. You can create a script that runs outside of MODx or a separate snippet that runs writeSchema() and parseSchema().

                You should end up with a /model directory and under that, a mysql directory. The actual locations of the schema file and the model directory are flexible (which can make things confusing), but there are some conventions. Here’s one typical structure:

                core
                  components
                     yourcomponent
                        model
                           yourcomponent
                               sometable.class.php
                               mysql
                                   sometable.class.php
                                   sometable.map.inc.php
                        schema
                           schemafile.xml


                It may be necessary to create the core/yourcomponent dir. and the core/yourcomponent/model dir. ahead of time.

                The following two methods will create everything else if you give them the right paths.

                writeSchema () writes the schema file based on the tables in your DB (or you can create the schema file manually).

                You give it the path where you want your schema file written and it writes it. It doesn’t care where.

                parseSchema() uses the schema file to create the rest of the files and directories. You give it the path to your schema file and the path to your /model/ directory -- it doesn’t care where they are.

                If you use the path structure above for the schema file and the model directory, you should end up with the rest of the files above where they are shown.

                At that point, you’re done with writeSchema() and parseSchema(). They’re only there to create the necessary files, which never change unless you modify the DB table(s) and re-run them.

                To use the classes, all you need to do is to put addPackage() at the top of your snippet and give it the full path to your model directory. The class name (the equivalent to modResource, modSnippet, etc.) will be shown in the schema file (class=’someclass’).

                If you want to change it, you can use writeSchema() alone, then edit the schema file to change the class name, then run parseSchema().

                I’m working on a tutorial for this, but it may be a while before it sees the light of day.



                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 19349
                  • 9 Posts
                  I need 1day to find out how it works, but now i have it. Thanks smiley
                    • 14372
                    • 49 Posts
                    Hey guys,

                    I have a formit call working with validation and everything (email) ... now I wanted to add the data to database ...

                    I followed the tutorial here, http://bobsguides.com/custom-db-tables.html

                    I created the classes and maps and added some sample data to the DB and tested getCollection which returned appropriate resutls. So in short, the object exists and retrieval works.

                    So onto step 2 ... I wanted to create a hook to formit that looks like this ...

                    <?php
                    
                    
                    /* Add ah_postings package */
                    
                    $path = MODX_CORE_PATH . 'components/ah_postings/';
                    $result = $modx->addPackage('ah_postings',$path . 'model/','vw_ah_pos_');
                    
                    
                    /* Create new posting object */
                    
                    $position = $modx -> newObject('Postings');
                    
                    
                    /* Get some FormIt values/fields with hook API*/
                    
                    $usr = $hook -> getValue('usr');
                    $department = $hook -> getValue('department');
                    $duration = $hook -> getValue('duration');
                    $renumeration = $hook -> getValue('renumeration');
                    $appmethod = $hook -> getValue('apply-method');
                    $interns = $hook -> getValue('interns');
                    $date = $hook -> getValue('date');
                    
                    /* Add each of the entries/fields to the object */
                    
                    $position->set('usr',$usr);
                    $position->set('department',$department);
                    $position->set('duration',$duration);
                    $position->set('renumeration',$renumeration);
                    $position->set('apply-method',$appmethod);
                    $position->set('interns',$interns);
                    $position->set('date',$date);
                    
                    
                    /* Save the object */
                    
                    $position->save();
                    
                    return true;


                    So that in my formit call I do this ...

                    &hooks=`snippetname,email`


                    Pretty simple really ... unlike some of the stuff that I’ve seen here e.g. formit2db and db2formit.

                    Thing is, the form still runs and an email is sent e.t.c ... validation still work and all that ... BUT nothing is added to the DB ...

                    Expected behaviour is that a row is added to the table ... anything that I’ve done wrong?
                      • 4172
                      • 5,888 Posts
                      check, if you got a new object with something like that:

                      if (!is_object($position) || !($position instanceof xPDOObject)) {
                          $errorMsg='Failed to create object of type:Postings ';
                          $hook->addError('error_message',$errorMsg); 
                          return false;
                      }
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!