Line 1:
Line 1:
+
<!-- BEGIN origin interlang links -->
+
<!-- You can edit this section but do NOT remove these comments
+
Links below will be automatically replicated on translated pages by PolyglotBot -->
+
[[fr:Objets_métiers]]
+
[[es:Objetos_de_negociado]]
+
[[zh:业务对象]]
+
<!-- END interlang links -->
+
{{TemplateDocDevEn}}
{{TemplateDocDevEn}}
= Business objects =
= Business objects =
All business objects are defined by a PHP class that inherits the class CommonObject (defined in file commonobject.class.php).
All business objects are defined by a PHP class that inherits the class CommonObject (defined in file commonobject.class.php).
−
Every business object has the 4 CRUD methods you can use to read or save database:
+
Every business object has the 4 CRUD methods you can use to read or save into database:
* Create: create()
* Create: create()
* Read: fetch()
* Read: fetch()
Line 11:
Line 19:
= Link between Business objects =
= Link between Business objects =
−
Some business objects are linked to other. This is done into Dolibarr using the [[Table llx_element_element]]. But you don't have to read this table to get objects linked to another.
+
Some business objects are linked to other business objects. This is done in Dolibarr using the [[Table llx_element_element]]. But you don't have to read this table to get objects linked to another.
−
This is a portion of code that shows how you can do this:
+
+
Here is how you can use this (Dolibarr 3.1 and higher):
+
<source lang="php">
+
// get invoices from an already loaded order:
+
$result = $order->fetchObjectLinked($order->id, 'commande', null, 'facture');
+
if ($result <= 0) {
+
// handle error
+
}
+
// important notice : the newly populated arrays are associative. Keys are llx_element_element row ids.
+
+
// now if we expect only one linked invoice:
+
+
// WRONG way to get it ($invoice_id will be NULL):
+
$invoice_id = $order->linkedObjectsIds['facture'][0]
+
+
// CORRECT way ($invoice_id will hold the ID):
+
$invoice_id = array_values($order->linkedObjectsIds['facture'])[0]
+
+
// Better (if there are more than one linked invoices) :
+
foreach ($order->linkedObjectsIds['facture'] as $element_element_id => $facture_id) {
+
// process it
+
}
+
</source>
+
+
Prior to Dolibarr < 3.1:
<source lang="php">
<source lang="php">
// This load the array linked_object with objects that link to this one
// This load the array linked_object with objects that link to this one