Hi Volker
Please forgive me my comment here. But it is generally not a good idea to calculate a result from 2 fields and put the result in a 3rd field of the same table. Better is to calculate on the fly whenever you need it.
Having said that you have several methods you could use depending on your use case. Among them, these methods here could be of interest:
calculate on the fly with a nodb schema:
you need to know how to create sql queries.
You might encounter problems only if you include it in the 'default' view in the main schema.- you create a nodb_schema with a sql-query that does the calculation on the fly
- you include this in the main schema like this:
<view name="some_name" template="display" schema="nodb_schema"></view>
create a trigger in the db to calculate and store
you need to know how to create triggers in your database.
You use the calculated field in the normal way in SGS. But be aware, SGS does not know about this trigger.
So you need to document well.
- create field 3 normally
- create a trigger to do the calculation and store the result in field 3
- use normally but take care to make field 3 non editable in SGS.
use a php class to implement a custom php function
you need to know how to program in PHP, how to define a class and how to define a static function within the class.
this receipe ensures that you have all the custom functions for a particular schema in a central place.
probably you need to test a little bit with $params to know how SGS fills them from the schema.
- create a custom class in file mySchema.php.
If your schema is called mySchema you could create a class like this:
class mySchema {
static function myFunction($id, $data, $params) {
// to some stuff, programming and calculation, etc;
// you can reference the fields like this: $data["field1"]
// eg: $data["field3"] = $data["field1"] * $data["field2"];
// to update the db you can use the function 'db_update'
// this function is defined in functions.php class sys
// but you can basically use any valid php method to store data in a db
}
}
- include a trigger_edit tag in your schema like this:
<table trigger_edit="mySchema::myFunction"></table>
Regards
Patrick