We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4172
    • 5,888 Posts
    here is an example how you can add and edit datas from custom-tables with formit.
    It can also create schema and classes, if not exists.
    made that for my own excersise. Perhaps others find that usefull too.

    use own tableprefix for your custom-tables.
    if you want to edit table-row add &resId=yourRowID to your URL <- replace yourRowID with id of tablerow

    formit-snippetcall:
    [[!FormIt? 
    &hooks=`spam,formit2db,email` 
    &preHooks=`db2formit`
    &prefix=`modx_regatta_`
    &packageName=`mypackage`
    &tablename=`ausschreibung`
    &emailTpl=`myEmailChunk` 
    &emailTo=`[email protected]`
    &validate=`
        nospam:blank,
        rennklasse:required,
        altersklasse:required`
    ]]
    
    <h2>Resource Form</h2>
    <p>[[+fi.error.error_message]]</p>
    <form class="form" action="[[~[[*id]]]]" method="post">
        <input name="nospam" type="hidden" />
        <input name="resource_id" type="hidden" value="[[+fi.id]]" />
        <label for="rennklasse">
            Rennklasse:
            <span class="error">[[+fi.error.rennklasse]]</span>
        </label>
        <input id="rennklasse" name="rennklasse" type="text" value="[[+fi.rennklasse]]" />    
        
    
        <label for="altersklasse">
            Altersklasse:
            <span class="error">[[+fi.error.altersklasse]]</span>
        </label>
        <input id="altersklasse" name="altersklasse" type="text" value="[[+fi.altersklasse]]" />
    <br class="clear" />
    <div class="form-buttons">
            <input type="submit" value="Save" />
        </div>
    </form>


    snippet db2formit:
    <?php
    
    $prefix=$scriptProperties['prefix'];
    $packageName = $scriptProperties['packageName'];
    $tablename = $scriptProperties['tablename'];
    $parseSchema = $scriptProperties['parseSchema'];
    
    $restrictPrefix = true;
    
    $packagepath = $modx->getOption('core_path') . 'components/'.$packageName.'/';
    $modelpath = $packagepath.'model/';
    $schemapath = $modelpath.'schema/';
    $schemafile = $schemapath.$packageName.'.mysql.schema.xml';
    $manager= $modx->getManager();
    $generator= $manager->getGenerator();
    if (!file_exists($schemafile)){
    
        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);
        $parseSchema = true;
       
    } 
    
    if ($parseSchema){
        //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 = $generator->getClassName($tablename);
    
    if (isset($_GET['resId'])){
        if ($dataobject=$modx->getObject($classname,array('id'=>$_GET['resId']))){
            if (!is_object($dataobject) || !($dataobject instanceof xPDOObject)) {
                $errorMsg='Failed to create object of type: ' . $classname;
                $hook->addError('error_message',$errorMsg); 
                return false;
            }
            $hook->setValues($dataobject->toArray());
        }
    
        //$errorMsg = '<pre>'.print_r($dataobject->toArray(),true).'</pre>';  
        //$hook->addError('error_message',$errorMsg);  
    }
    
    
    return true;


    snippet formit2db:
    <?php
    $prefix=$scriptProperties['prefix'];
    $packageName = $scriptProperties['packageName'];
    $tablename = $scriptProperties['tablename'];
    
    $packagepath = $modx->getOption('core_path') . 'components/'.$packageName.'/';
    $modelpath = $packagepath.'model/';
    
    $modx->addPackage($packageName,$modelpath,$prefix);
    $manager= $modx->getManager();
    $generator= $manager->getGenerator();
    $classname = $generator->getClassName($tablename);
    
    //$errorMsg = '<pre>'.print_r($scriptProperties['fields'],true).'</pre>';  
    //$hook->addError('error_message',$errorMsg);  
    
    //$scriptProperties['hook']->formit->config['emailTo']='[email protected]';
    $dataobject=$modx->getObject($classname,array('id'=>$hook->getValue('resource_id')));
    
    if (empty($dataobject)){
        $dataobject = $modx->newObject($classname);
        //$dataobject->set('createdby', $modx->user->get('id'));
    }
    else{
        //$dataobject->set('editedby', $modx->user->get('id'));
    }
    if (!is_object($dataobject) || !($dataobject instanceof xPDOObject)) {
        $errorMsg='Failed to create object of type: ' . $classname;
        $hook->addError('error_message',$errorMsg); 
        return false;
    }
    
    $allFormFields = $hook->getValues();    
    foreach ($allFormFields as $field=>$value){
        
        if ($field !== 'spam' && $field !== 'resource_id'){
            $dataobject->set($field,$value);
        }       
    }
    
    $dataobject->save();
    
    return true;


    there is also a version for resources and TVs on the forums: http://modxcms.com/forums/index.php/topic,54204.msg313114.html#msg313114

    If you have changed something on your schema after map-files are created add &parseSchema=`1` to the Formit-snippet-call to recreate your map-files.
    Don't forget to remove them when done. [ed. note: Bruno17 last edited this post 11 years, 11 months ago.]
      -------------------------------

      you can buy me a beer, if you like MIGX

      http://webcmsolutions.de/migx.html

      Thanks!
      • 4971
      • 964 Posts
      Bruno, you should hide the email in the formit call...
        Website: www.mercologia.com
        MODX Revo Tutorials:  www.modxperience.com

        MODX Professional Partner
        • 4172
        • 5,888 Posts
        Uups, thanks charliez!
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 20413
          • 2,877 Posts
          .:sweet:. smiley
            @hawproductions | http://mrhaw.com/

            Infograph: MODX Advanced Install in 7 steps:
            http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

            Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
            http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
            • 4172
            • 5,888 Posts
            updated code with new formit-api-calls
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 14267
              • 113 Posts
              Great! Now what would it take to add json or something to this?
                • 21193
                • 9 Posts
                Somebody can explain how to use this please? What kind of code is in the model/ folder?
                Can someone make a tutorial about how to save a simple form in a custom table db?
                Thanks
                  • 3749
                  • 24,544 Posts
                  Quote from: neopablix at Oct 14, 2010, 05:32 PM

                  Somebody can explain how to use this please? What kind of code is in the model/ folder?
                  Can someone make a tutorial about how to save a simple form in a custom table db?
                  Thanks

                  The code in the model folder is created by the parseSchema() call which writes it based on the schema file (created by writeSchema()) . It contains the class definitions and maps.
                    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
                    • 3749
                    • 24,544 Posts
                    Bruno17. Very cool. That will be really handy. I’m working on a tutorial on creating xPDO classes from existing DB tables (as soon as I can get past this: http://modxcms.com/forums/index.php/topic,55756.msg320660.html#msg320660 ).

                    Just to let you know, I think I have a slightly modified version of writeSchema() that will allow you to specify the tables to use and the class names to use with them as an extra final argument. As it is, if the custom tables are in the modx DB with the same prefix, your package ends up duplicating the entire modx object model.
                      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
                      • 4172
                      • 5,888 Posts
                      hey Bob,
                      I’m about to create a flexible CMP where one can edit custom-tables with configurable forms (fields can be TV-input-types).
                      I want also include a write/parse-schema processor
                      For that I could use your modified writeschema().
                      Can I get it somewhere?

                      Thanks!
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!