Module CustomFields Dev: Difference between revisions

Jump to navigation Jump to search
(Added more CustomFields API infos)
Line 268: Line 268:
<source lang="php">
<source lang="php">
$rtncode = $customfields->fetchAllFieldsStruct($nohide=false, $table=null, $cachepath=null);
$rtncode = $customfields->fetchAllFieldsStruct($nohide=false, $table=null, $cachepath=null);
</source>
== Managing custom fields records ==
Now that you know how to create and manage the structure of custom fields, we can proceed on to managing the records.
=== Creating a custom field's record ===
Simply use the create() method:
<source lang="php">
$rtncode = $customfields->create($object);
</source>
$object must be an object containing as its properties the customfields data.
For example, if you have a field named mycustomfield, your object must have $object->mycustomfields defined.
This method will store in the database ALL the custom fields that were created/edited and with a valid value (eg: an empty value for a field that does not accept null value is invalid) AT ONCE! So you can modify all the columns of one record at once, but not several records at once.
Concretely, this is very useful with Dolibarr, because modules tends to store everything inside a single object generally called $object, and CustomFields tries to hook onto $object, so that you can just pass $object as is to create() and it will save all the custom fields.
But you can also create an empty object with $object = new stdClass(); and then manually fill the custom fields records with whatever you want, and then call the create() method, this will work as well!
You can also just modify only one column of one record, by just specifying one custom field column_name in your $object. The other custom fields that are not specified won't be created/modified.
=== Updating a custom field's record ===
Use the update() method, which is an alias for the create() method:
<source lang="php">
$rtncode = $customfields->update($object);
</source>
As noted above, update() is an alias of the create() method, which means that CustomFields will try to detect by itself if a record needs to be updated or created.
=== Deleting a custom field's record ===
Use the delete() method:
<source lang="php">
$rtncode = $customfields->delete($id); // where $id is = to fk_moduleid column, NOT rowid
</source>
</source>


439

edits

Navigation menu