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 12 years, 6 months ago.]