100 public function create($user, $notrigger = 0)
102 global $conf, $langs;
105 $sql =
"INSERT INTO ".$this->db->prefix().$this->table_element.
"(";
110 $sql .=
") VALUES (";
111 $sql .=
" ".(!isset($this->
id) ?
'NULL' : ((int) $this->
id)).
",";
112 $sql .=
" ".(!isset($this->code) ?
'NULL' : ((int) $this->code)).
",";
113 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape(trim($this->label)).
"'").
",";
114 $sql .=
" ".(!isset($this->active) ?
'NULL' : ((int) $this->active)).
",";
119 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
120 $resql = $this->db->query($sql);
123 dol_syslog(get_class($this).
"::create ".$this->db->lasterror(), LOG_ERR);
124 $this->error =
"Error ".$this->db->lasterror();
125 $this->db->rollback();
128 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
142 public function fetch($id, $code =
'')
151 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
152 $sql_where = array();
154 $sql_where[] =
" t.rowid = ".((int) $id);
157 $sql_where[] =
" t.code = ".((int) $code);
159 if (count($sql_where) > 0) {
160 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
163 $resql = $this->db->query($sql);
165 if ($this->db->num_rows($resql)) {
166 $obj = $this->db->fetch_object($resql);
168 $this->
id = $obj->rowid;
169 $this->code = $obj->code;
170 $this->label = $obj->label;
171 $this->active = $obj->active;
173 $this->db->free($resql);
177 $this->error =
"Error ".$this->db->lasterror();
194 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
205 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
208 if (count($filter) > 0) {
209 foreach ($filter as $key => $value) {
210 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.code') {
211 $sqlwhere[] = $key.
" = ".((int) $value);
212 } elseif (strpos($key,
'date') !==
false) {
213 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
214 } elseif ($key ==
't.label') {
215 $sqlwhere[] = $key.
" = '".$this->db->escape($value).
"'";
217 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
221 if (count($sqlwhere) > 0) {
222 $sql .=
' WHERE ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
225 if (!empty($sortfield)) {
226 $sql .= $this->db->order($sortfield, $sortorder);
228 if (!empty($limit)) {
229 $sql .= $this->db->plimit($limit, $offset);
232 $resql = $this->db->query($sql);
234 $this->records = array();
235 $num = $this->db->num_rows($resql);
237 while ($obj = $this->db->fetch_object($resql)) {
238 $record =
new self($this->db);
240 $record->id = $obj->rowid;
241 $record->code = $obj->code;
242 $record->label = $obj->label;
243 $this->records[$record->id] = $record;
246 $this->db->free($resql);
248 return $this->records;
250 $this->errors[] =
'Error '.$this->db->lasterror();
251 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
265 public function update($user =
null, $notrigger = 0)
267 global $conf, $langs;
270 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
271 $sql .=
" code=".(isset($this->code) ? ((int) $this->code) :
"null").
",";
272 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape(trim($this->label)).
"'" :
"null").
",";
273 $sql .=
" active=".(isset($this->active) ? ((int) $this->active) :
"null");
274 $sql .=
" WHERE rowid=".(int) $this->
id;
278 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
279 $resql = $this->db->query($sql);
282 dol_syslog(get_class($this).
"::update Error ".$this->db->lasterror(), LOG_ERR);
283 $this->error =
"Error ".$this->db->lasterror();
284 $this->db->rollback();