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
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.