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
    if you have formit2db from repository, then you need

    &packagename=`doodles`
    &classname=`Doodle`


    If you want to store informations into more than one table at the same time, you will need a customized version of the formit2db - snippet.
      -------------------------------

      you can buy me a beer, if you like MIGX

      http://webcmsolutions.de/migx.html

      Thanks!
      • 33238
      • 388 Posts
      Thank you Bruno.

      Bruno, can you guide me a little bit about how this works?
      because:

      - I don't need to modify the schema or the table already created: modx_doodles
      - Everything must go to the table: modx_doodles (just one table)
      - the table has 2 fields: name and description (and teh other ones like createdon, etc...)
      - But I have 3 fields in my form (is more than that but this is the example): name, email and comment

      in this case, the values of the field name is going to be save in the table (modx_doodles) in the column: name
      but my question is (and here is where I need your help): how can I save the fields email and comment together in the column: description?

      I'm using your code, can you give a small guide please? (I'm not expert in this area, but I will wink

      I'm not using: $prefix=$scriptProperties['prefix']; because my prefix is the default one, or do I need to use it like:
      $prefix=$scriptProperties['modx_'];?

      <?php
      $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();
      


      And the last question:

      How form2db knows where to save the info from every form field to the database columns?

      both things must share the same name? e.g.:

      <input name="name" type="text" value="blablabla" />
      BD: <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>

      what happend if the imput is call: <input name="FULLname"
      and the BD: <field key="name"... ?

      I'm not sure if is an stupid questions but, how it works?

      Thank you very much Bruno.
        --
        ysanmiguel.com
        • 4172
        • 5,888 Posts
        I wouldn't store email and comment into one field.

        It is easy to add new fields by extending the xpdo-schema with additional fields and let MIGX (MIGX CMP package manager) add this fields to your table and the map-files. There is a button 'Add fields' which does add additional field to your table, if they are in the xpdo-schema.

        And yes, the input-names in your form needs to be the same as the fieldnames in your table.
        Then you don't need to change anything in formit2db.

        Install and use the version of formit2db from Extras-Repo, which you can install from MODX package-manager, if you haven't allready!
        Not the one from the forum-post, if you have that one.
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
        • db2formit retreives all fields from one xPDO class row with a where xPDO where clause. formit2db saves all fields to one xPDO class row (if you set a xPDO where clause you could update a row).

          So if you want to use your own name tags, you have to write a custom hook that transforms the fieldnames before the formit2db hook (or after the db2formit hook, if you use that).

          $hook->setValue('name') = $hook->getValue('FULLname')
          • Just an idea: formit2db/db2formit could be easy extended that it could use custom (one to one) composite relations. The name attribute of the form field could contain the classname and a dividing char as a prefix. Not sure which char could be used as dividing char.
              • 4172
              • 5,888 Posts
              Say you have something like

              &classname=`modUser`
              &joins='{"alias":"Profile"}'


              db2formit could produce fieldnames like 'Profile_fullname'

              Then you can read the joins - array and put all fields with the found prefix of the given alias 'Profile_'
              to this table.

              I do it often this way in MIGXdb custom-update-processors.

                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 33238
                • 388 Posts
                OK thank you very much for the information. I will try that and I will let you know what happen.

                Thank you Bruno and Jako.
                  --
                  ysanmiguel.com
                • @Bruno: Something like that. The dividing sign has to be configurable too.