Add method documentations

This commit is contained in:
atm-ph 2017-03-25 10:48:06 +01:00
parent de832d90a9
commit 269645dfc7

View File

@ -28,13 +28,23 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
class CoreObject extends CommonObject class CoreObject extends CommonObject
{ {
public $withChild = true; public $withChild = true;
/**
* @var string Error string
*/
public $error = ''; public $error = '';
/*
/**
* @var string[] Array of error strings
*/
public $errors=array();
/**
* @var Array $_fields Fields to synchronize with Database * @var Array $_fields Fields to synchronize with Database
*/ */
protected $__fields=array(); protected $__fields=array();
/**
/**
* Constructor * Constructor
* *
* @param DoliDB $db Database handler * @param DoliDB $db Database handler
@ -338,6 +348,7 @@ class CoreObject extends CommonObject
else else
{ {
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
$this->errors[] = $this->error;
return false; return false;
} }
} }
@ -397,148 +408,202 @@ class CoreObject extends CommonObject
} }
public function fetchChild() /**
* Function to fetch children objects
*/
public function fetchChild()
{ {
if($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
{
foreach($this->childtables as &$childTable)
{
$className = ucfirst($childTable);
if($this->withChild && !empty($this->childtables) && !empty($this->fk_element)) { $this->{$className}=array();
foreach($this->childtables as &$childTable) {
$className = ucfirst($childTable);
$this->{$className}=array();
$sql = " SELECT rowid FROM ".MAIN_DB_PREFIX.$childTable." WHERE ".$this->fk_element."=".$this->id;
$res = $this->db->query($sql);
if($res) {
$Tab=array();
while($obj = $this->db->fetch_object($res)) {
$o=new $className($this->db);
$o->fetch($obj->rowid);
$this->{$className}[] = $o;
}
}
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$childTable.' WHERE '.$this->fk_element.' = '.$this->id;
$res = $this->db->query($sql);
if($res)
{
while($obj = $this->db->fetch_object($res))
{
$o=new $className($this->db);
$o->fetch($obj->rowid);
$this->{$className}[] = $o;
}
}
else
{
$this->errors[] = $this->db->lasterror();
}
} }
} }
} }
public function saveChild(User &$user) { /**
* Function to update children data
if($this->withChild && !empty($this->childtables) && !empty($this->fk_element)) { *
foreach($this->childtables as &$childTable) { * @param User $user user object
*/
public function saveChild(User &$user)
{
if($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
{
foreach($this->childtables as &$childTable)
{
$className = ucfirst($childTable); $className = ucfirst($childTable);
if(!empty($this->{$className})) { if(!empty($this->{$className}))
foreach($this->{$className} as $i => &$object) { {
foreach($this->{$className} as $i => &$object)
{
$object->{$this->fk_element} = $this->id; $object->{$this->fk_element} = $this->id;
$object->update($user); $object->update($user);
if($this->unsetChildDeleted && isset($object->to_delete) && $object->to_delete==true) unset($this->{$className}[$i]); if($this->unsetChildDeleted && isset($object->to_delete) && $object->to_delete==true) unset($this->{$className}[$i]);
} }
} }
} }
} }
} }
public function update(User &$user) {
if(empty($this->id )) return $this->create($user); // To test, with that, no need to test on high level object, the core decide it, update just needed
/**
if(isset($this->to_delete) && $this->to_delete==true) { * Function to update object or create or delete if needed
$this->delete($user); *
} * @param User $user user object
else { * @return < 0 if ko, > 0 if ok
*/
$query = array(); public function update(User &$user)
{
$query = $this->set_save_query(); if (empty($this->id)) return $this->create($user); // To test, with that, no need to test on high level object, the core decide it, update just needed
$query['rowid']=$this->id; elseif (isset($this->to_delete) && $this->to_delete==true) return $this->delete($user);
$res = $this->db->update($this->table_element,$query,array('rowid')); $error = 0;
$this->db->begin();
if($res) {
$query = $this->set_save_query();
$result = $this->call_trigger(strtoupper($this->element). '_UPDATE', $user); $query['rowid'] = $this->id;
$this->saveChild($user); $res = $this->db->update($this->table_element, $query, array('rowid'));
if ($res)
return true; {
} $result = $this->call_trigger(strtoupper($this->element). '_UPDATE', $user);
else{ if ($result < 0) $error++;
$this->error = $this->db->lasterror(); else $this->saveChild($user);
}
return false; else
} {
$error++;
} $this->error = $this->db->lasterror();
return $this->id; $this->errors[] = $this->error;
}
if (empty($error))
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
} }
public function create(User &$user) {
if($this->id>0) return $this->update($user); /**
* Function to create object in database
$query = array(); *
* @param User $user user object
* @return < 0 if ko, > 0 if ok
*/
public function create(User &$user)
{
if($this->id > 0) return $this->update($user);
$error = 0;
$this->db->begin();
$query = $this->set_save_query(); $query = $this->set_save_query();
$query['datec'] = date("Y-m-d H:i:s",$this->datec); $query['datec'] = date("Y-m-d H:i:s", $this->datec);
$res = $this->db->insert($this->table_element,$query); $res = $this->db->insert($this->table_element, $query);
if($res)
if($res) { {
$this->id = $this->db->last_insert_id($this->table_element); $this->id = $this->db->last_insert_id($this->table_element);
$result = $this->call_trigger(strtoupper($this->element). '_CREATE', $user); $result = $this->call_trigger(strtoupper($this->element). '_CREATE', $user);
if ($result < 0) $error++;
$this->saveChild($user); else $this->saveChild($user);
return $this->id;
} }
else{ else
{
$this->error = $this->db->lasterror(); $error++;
$this->error = $this->db->lasterror();
return false; $this->errors[] = $this->error;
} }
if (empty($error))
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
} }
/**
* Function to delete object in database
*
* @param User $user user object
* @return < 0 if ko, > 0 if ok
*/
public function delete(User &$user) public function delete(User &$user)
{ {
if($this->id>0) if ($this->id <= 0) return 0;
{
$error = 0;
$result = $this->call_trigger(strtoupper($this->element). '_DELETE', $user); $error = 0;
if ($result < 0) $error++; $this->db->begin();
$result = $this->call_trigger(strtoupper($this->element). '_DELETE', $user);
if ($result < 0) $error++;
if (!$error) if (!$error)
{
$this->db->delete($this->table_element, array('rowid' => $this->id), array('rowid'));
if($this->withChild && !empty($this->childtables))
{ {
$this->db->delete($this->table_element, array('rowid'=>$this->id), array('rowid')); foreach($this->childtables as &$childTable)
if($this->withChild && !empty($this->childtables))
{ {
foreach($this->childtables as &$childTable) $className = ucfirst($childTable);
if (!empty($this->{$className}))
{ {
$className = ucfirst($childTable); foreach($this->{$className} as &$object)
if (!empty($this->{$className}))
{ {
foreach($this->{$className} as &$object) $object->delete($user);
{
$object->delete($user);
}
} }
} }
} }
} }
}
} if (empty($error))
{
$this->db->commit();
return 1;
}
else
{
$this->error = $this->db->lasterror();
$this->errors[] = $this->error;
$this->db->rollback();
return -1;
}
} }
@ -554,7 +619,6 @@ class CoreObject extends CommonObject
if(empty($this->{$field})) return ''; if(empty($this->{$field})) return '';
else else
{ {
return dol_print_date($this->{$field}, $format); return dol_print_date($this->{$field}, $format);
} }
} }