Line 20:
Line 20:
= Link between Business objects =
= Link between Business objects =
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.
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