41 public $element =
'undefined';
46 public $table_element =
'undefined';
51 public $lines = array();
87 $fieldlabel =
'label';
88 if ($this->table_element ==
'c_stcomm') {
89 $fieldlabel =
'libelle';
90 } elseif ($this->table_element ==
'c_type_fees') {
98 if (isset($this->code)) {
99 $this->code = trim($this->code);
101 if (isset($this->label)) {
102 $this->label = trim($this->label);
104 if (isset($this->active)) {
105 $this->active = trim($this->active);
109 $sql =
'INSERT INTO '.$this->db->prefix().$this->table_element.
'(';
113 $sql .=
') VALUES (';
114 $sql .=
' '.(!isset($this->code) ?
'NULL' :
"'".$this->db->escape($this->code).
"'").
',';
115 $sql .=
' '.(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
',';
116 $sql .=
' '.(!isset($this->active) ?
'NULL' : $this->active);
121 $resql = $this->db->query($sql);
124 $this->errors[] =
'Error '.$this->db->lasterror();
125 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
129 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
144 $this->db->rollback();
163 public function fetch($id, $code =
'', $label =
'')
167 $fieldrowid =
'rowid';
168 $fieldlabel =
'label';
169 if ($this->table_element ==
'c_stcomm') {
171 $fieldlabel =
'libelle';
172 } elseif ($this->table_element ==
'c_type_fees') {
177 $sql .=
" t.".$fieldrowid.
",";
179 $sql .=
" t.".$fieldlabel.
" as label,";
181 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
183 $sql .=
" WHERE t.".$fieldrowid.
" = ".((int) $id);
185 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
187 $sql .=
" WHERE t.label = '".$this->db->escape($label).
"'";
190 $resql = $this->db->query($sql);
192 $numrows = $this->db->num_rows($resql);
194 $obj = $this->db->fetch_object($resql);
196 $this->
id = $obj->$fieldrowid;
198 $this->code = $obj->code;
199 $this->label = $obj->label;
200 $this->active = $obj->active;
209 $this->db->free($resql);
217 $this->errors[] =
'Error '.$this->db->lasterror();
218 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
236 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
240 $fieldrowid =
'rowid';
241 $fieldlabel =
'label';
242 if ($this->table_element ==
'c_stcomm') {
244 $fieldlabel =
'libelle';
245 } elseif ($this->table_element ==
'c_type_fees') {
250 $sql .=
" t.".$fieldrowid.
",";
252 $sql .=
" t.".$fieldlabel.
" as label,";
254 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
258 if (count($filter) > 0) {
259 foreach ($filter as $key => $value) {
260 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
264 if (count($sqlwhere) > 0) {
265 $sql .=
" WHERE ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
267 if (!empty($sortfield)) {
268 $sql .= $this->db->order($sortfield, $sortorder);
270 if (!empty($limit)) {
271 $sql .= $this->db->plimit($limit, $offset);
274 $resql = $this->db->query($sql);
276 $num = $this->db->num_rows($resql);
278 while ($obj = $this->db->fetch_object($resql)) {
279 $line =
new self($this->db);
281 $line->id = $obj->$fieldrowid;
283 $line->code = $obj->code;
284 $line->label = $obj->label;
285 $line->active = $obj->active;
287 $this->db->free($resql);
291 $this->errors[] =
'Error '.$this->db->lasterror();
292 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
312 $fieldrowid =
'rowid';
313 $fieldlabel =
'label';
314 if ($this->table_element ==
'c_stcomm') {
316 $fieldlabel =
'libelle';
317 } elseif ($this->table_element ==
'c_type_fees') {
323 if (isset($this->code)) {
324 $this->code = trim($this->code);
326 if (isset($this->label)) {
327 $this->label = trim($this->label);
329 if (isset($this->active)) {
330 $this->active = trim($this->active);
337 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
' SET';
338 $sql .=
" code = ".(isset($this->code) ?
"'".$this->db->escape($this->code).
"'" :
"null").
',';
339 $sql .=
" ".$fieldlabel.
" = ".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
',';
340 $sql .=
" active = ".(isset($this->active) ? $this->active :
"null");
341 $sql .=
" WHERE ".$fieldrowid.
" = ".((int) $this->
id);
345 $resql = $this->db->query($sql);
348 $this->errors[] =
'Error '.$this->db->lasterror();
349 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
364 $this->db->rollback();
382 public function delete(
User $user, $notrigger =
false)
386 $fieldrowid =
'rowid';
405 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
406 $sql .=
" WHERE ".$fieldrowid.
" = ".((int) $this->
id);
408 $resql = $this->db->query($sql);
411 $this->errors[] =
'Error '.$this->db->lasterror();
412 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
418 $this->db->rollback();
445 $object->fetch($fromid);
453 $object->context[
'createfromclone'] =
'createfromclone';
454 $result = $object->create($user);
459 $this->errors = $object->errors;
460 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
463 unset($object->context[
'createfromclone']);
471 $this->db->rollback();
487 $this->code =
'CODE';
488 $this->label =
'Label';
create(User $user, $notrigger=false)
Create object into database.
update(User $user, $notrigger=false)
Update object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
fetch($id, $code='', $label='')
Load object in memory from the database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
__construct(DoliDB $db)
Constructor.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.