Add option to not load all object and fix so linkedObjectsIds has same

content than linkedObjects
This commit is contained in:
Laurent Destailleur 2018-09-28 17:38:29 +02:00
parent a3065ae612
commit f667270d4d

View File

@ -2930,7 +2930,9 @@ abstract class CommonObject
} }
/** /**
* Fetch array of objects linked to current object. Links are loaded into this->linkedObjects array and this->linkedObjectsIds * Fetch array of objects linked to current object (object of enabled modules only). Links are loaded into
* this->linkedObjectsIds array and
* this->linkedObjects array if $loadalsoobjects = 1
* Possible usage for parameters: * Possible usage for parameters:
* - all parameters empty -> we look all link to current object (current object can be source or target) * - all parameters empty -> we look all link to current object (current object can be source or target)
* - source id+type -> will get target list linked to source * - source id+type -> will get target list linked to source
@ -2943,12 +2945,13 @@ abstract class CommonObject
* @param int $targetid Object target id (if not defined, id of object) * @param int $targetid Object target id (if not defined, id of object)
* @param string $targettype Object target type (if not defined, elemennt name of object) * @param string $targettype Object target type (if not defined, elemennt name of object)
* @param string $clause 'OR' or 'AND' clause used when both source id and target id are provided * @param string $clause 'OR' or 'AND' clause used when both source id and target id are provided
* @param int $alsosametype 0=Return only links to object that differs from source. 1=Include also link to objects of same type. * @param int $alsosametype 0=Return only links to object that differs from source type. 1=Include also link to objects of same type.
* @param string $orderby SQL 'ORDER BY' clause * @param string $orderby SQL 'ORDER BY' clause
* @param int $loadalsoobjects Load also array this->linkedObjects (Use 0 to increase performances)
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
* @see add_object_linked, updateObjectLinked, deleteObjectLinked * @see add_object_linked, updateObjectLinked, deleteObjectLinked
*/ */
function fetchObjectLinked($sourceid=null,$sourcetype='',$targetid=null,$targettype='',$clause='OR',$alsosametype=1,$orderby='sourcetype') function fetchObjectLinked($sourceid=null,$sourcetype='',$targetid=null,$targettype='',$clause='OR',$alsosametype=1,$orderby='sourcetype',$loadalsoobjects=1)
{ {
global $conf; global $conf;
@ -3042,7 +3045,8 @@ abstract class CommonObject
if (! empty($this->linkedObjectsIds)) if (! empty($this->linkedObjectsIds))
{ {
foreach($this->linkedObjectsIds as $objecttype => $objectids) // $objecttype is a module name ('facture', 'mymodule', ...) or a module name with a suffix ('project_task', 'mymodule_myobj', ...) $tmparray = $this->linkedObjectsIds;
foreach($tmparray as $objecttype => $objectids) // $objecttype is a module name ('facture', 'mymodule', ...) or a module name with a suffix ('project_task', 'mymodule_myobj', ...)
{ {
// Parse element/subelement (ex: project_task, cabinetmed_consultation, ...) // Parse element/subelement (ex: project_task, cabinetmed_consultation, ...)
$module = $element = $subelement = $objecttype; $module = $element = $subelement = $objecttype;
@ -3107,6 +3111,8 @@ abstract class CommonObject
// Here $module, $classfile and $classname are set // Here $module, $classfile and $classname are set
if ($conf->$module->enabled && (($element != $this->element) || $alsosametype)) if ($conf->$module->enabled && (($element != $this->element) || $alsosametype))
{
if ($loadalsoobjects)
{ {
dol_include_once('/'.$classpath.'/'.$classfile.'.class.php'); dol_include_once('/'.$classpath.'/'.$classfile.'.class.php');
//print '/'.$classpath.'/'.$classfile.'.class.php '.class_exists($classname); //print '/'.$classpath.'/'.$classfile.'.class.php '.class_exists($classname);
@ -3124,6 +3130,11 @@ abstract class CommonObject
} }
} }
} }
else
{
unset($this->linkedObjectsIds[$objecttype]);
}
}
} }
return 1; return 1;
} }