69 public function create($user, $notrigger = 0)
74 $sql =
"INSERT INTO ".$this->db->prefix().$this->table_element.
"(";
80 $sql .=
" ".(!isset($this->
id) ?
'NULL' : ((int) $this->
id)).
",";
81 $sql .=
" ".(!isset($this->code) ?
'NULL' : ((int) $this->code)).
",";
82 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape(trim($this->label)).
"'").
",";
83 $sql .=
" ".(!isset($this->active) ?
'NULL' : ((int) $this->active)).
",";
88 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
89 $resql = $this->db->query($sql);
92 dol_syslog(get_class($this).
"::create ".$this->db->lasterror(), LOG_ERR);
93 $this->error =
"Error ".$this->db->lasterror();
94 $this->db->rollback();
97 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
111 public function fetch($id, $code =
'')
120 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
121 $sql_where = array();
123 $sql_where[] =
" t.rowid = ".((int) $id);
126 $sql_where[] =
" t.code = ".((int) $code);
128 if (count($sql_where) > 0) {
129 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
132 $resql = $this->db->query($sql);
134 if ($this->db->num_rows($resql)) {
135 $obj = $this->db->fetch_object($resql);
137 $this->
id = $obj->rowid;
138 $this->code = $obj->code;
139 $this->label = $obj->label;
140 $this->active = $obj->active;
142 $this->db->free($resql);
146 $this->error =
"Error ".$this->db->lasterror();
163 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
172 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
173 $sql .=
" WHERE 1 = 1";
176 if (is_array($filter)) {
178 if (count($filter) > 0) {
179 foreach ($filter as $key => $value) {
180 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.code') {
181 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
182 } elseif (strpos($key,
'date') !==
false) {
183 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
184 } elseif ($key ==
't.label') {
185 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
187 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($value).
"%'";
191 if (count($sqlwhere) > 0) {
192 $sql .=
" AND ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
202 $this->errors[] = $errormessage;
203 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
207 if (!empty($sortfield)) {
208 $sql .= $this->db->order($sortfield, $sortorder);
210 if (!empty($limit)) {
211 $sql .= $this->db->plimit($limit, $offset);
214 $resql = $this->db->query($sql);
216 $this->records = array();
217 $num = $this->db->num_rows($resql);
219 while ($obj = $this->db->fetch_object($resql)) {
220 $record =
new self($this->db);
222 $record->id = $obj->rowid;
223 $record->code = $obj->code;
224 $record->label = $obj->label;
225 $this->records[$record->id] = $record;
228 $this->db->free($resql);
230 return $this->records;
232 $this->errors[] =
'Error '.$this->db->lasterror();
233 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
247 public function update($user =
null, $notrigger = 0)
249 global $conf, $langs;
252 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
253 $sql .=
" code=".(isset($this->code) ? ((int) $this->code) :
"null").
",";
254 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape(trim($this->label)).
"'" :
"null").
",";
255 $sql .=
" active=".(isset($this->active) ? ((int) $this->active) :
"null");
256 $sql .=
" WHERE rowid=".(int) $this->
id;
260 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
261 $resql = $this->db->query($sql);
264 dol_syslog(get_class($this).
"::update Error ".$this->db->lasterror(), LOG_ERR);
265 $this->error =
"Error ".$this->db->lasterror();
266 $this->db->rollback();