Changes

Line 557: Line 557:     
Powertip: you can mix this automatic feature with the NonEditable option, the Hide option and the Duplication option: this will allow you to make a bridge and access any object. For example, in your Invoices, if you want to access in your ODT document every fields of the Client (most of the client's infos are not accessible in ODT documents), you can create a custom field that will duplicate the client's id field, and then in your ODT document you will gain access to any Client's fields thank's to the RRFA feature. If in addition you put this custom field as NonEditable and Hide, this custom field will be totally transparent and hidden, your Dolibarr users won't even notice it.
 
Powertip: you can mix this automatic feature with the NonEditable option, the Hide option and the Duplication option: this will allow you to make a bridge and access any object. For example, in your Invoices, if you want to access in your ODT document every fields of the Client (most of the client's infos are not accessible in ODT documents), you can create a custom field that will duplicate the client's id field, and then in your ODT document you will gain access to any Client's fields thank's to the RRFA feature. If in addition you put this custom field as NonEditable and Hide, this custom field will be totally transparent and hidden, your Dolibarr users won't even notice it.
 +
 +
== Manage constrained fields programmatically ==
 +
 +
If you use the facade API customfields_fill_object(), the constrained fields will be automatically managed for you, you won't have to do any further processing.
 +
 +
However, if you want to have a finer control over constrained fields, read the rest of this chapter.
 +
 +
Constrained custom fields are just like any custom field: you can $customfields->fetch() it, and also $customfields->fetchFieldStruct() to get its options.
 +
 +
Example to load the constrained custom field structure and check if it's really constrained:
 +
 +
<source lang="php">
 +
dol_include_once('/customfields/class/customfields.class.php'); // NEW WAY since Dolibarr v3.3
 +
$customfields = new CustomFields($this->db, $currentmodule); // where $currentmodule is the current module, you can replace it by '' if you just want to use printing functions and fetchAny.
 +
$myfield = $customfields->fetchFieldStruct('myfield'); // where 'myfield' is the name of the constrained custom field
 +
 +
// Check if it's a constrained field
 +
if ($myfield->referenced_table_name) {
 +
    // Do your stuff here...
 +
}
 +
</source>
 +
 +
Now, if you want to fetch the remote fields referenced by your constrained customfield, use one of the following:
 +
 +
<source lang="php">
 +
//$fkrecord = $customfields->fetchAny('*', $field->referenced_table_name, $field->referenced_column_name."='".$id."'"); // we fetch the record in the referenced table. Equivalent to the fetchReferencedValuesList command below using $id=$value and $allcolumns=true.
 +
//$fkrecord = $customfields->fetchReferencedValuesList($field, $id, null, true); // works correctly but is not recursive. Use fetchReferencedValuesRec() for a recursive fetching.
 +
$fkrecord = fetchReferencedValuesRec($customfields, $field, $id); // we fetch the record in the referenced table, in a recursive fashion.
 +
</source>
 +
 +
where $id is the id of the record you want to fetch, generally given by $object->cf_myfield.
 +
 +
For more info on these functions, see the developper's documentation.
 +
 +
From here, you have an $fkrecord array containing all remote fields values, and you can do whatever you want with those.
    
= Extra options =
 
= Extra options =
439

edits