79 public function create($user, $notrigger = 0)
82 $sql =
"INSERT INTO ".$this->db->prefix().$this->table_element.
"(";
90 $sql .=
" ".(!isset($this->
id) ?
'NULL' : ((int) $this->
id)).
",";
91 $sql .=
" ".(!isset($this->code) ?
'NULL' : ((int) $this->code)).
",";
92 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape(trim($this->label)).
"'").
",";
94 $sql .=
" ".(!isset($this->percent) ?
'NULL' : (float) $this->percent).
",";
95 $sql .=
" ".(!isset($this->active) ?
'NULL' : ((int) $this->active)).
",";
100 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
101 $resql = $this->db->query($sql);
104 dol_syslog(get_class($this).
"::create ".$this->db->lasterror(), LOG_ERR);
105 $this->error =
"Error ".$this->db->lasterror();
106 $this->db->rollback();
109 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
123 public function fetch($id, $code =
'')
129 $sql .=
" t.position,";
130 $sql .=
" t.percent,";
132 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
133 $sql_where = array();
135 $sql_where[] =
" t.rowid = ".((int) $id);
138 $sql_where[] =
" t.code = ".((int) $code);
140 if (count($sql_where) > 0) {
141 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
144 $resql = $this->db->query($sql);
146 if ($this->db->num_rows($resql)) {
147 $obj = $this->db->fetch_object($resql);
149 $this->
id = $obj->rowid;
150 $this->code = $obj->code;
151 $this->label = $obj->label;
153 $this->percent = $obj->percent;
154 $this->active = $obj->active;
156 $this->db->free($resql);
160 $this->error =
"Error ".$this->db->lasterror();
177 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
185 $sql .=
" t.position,";
186 $sql .=
" t.percent,";
188 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
189 $sql .=
" WHERE 1 = 1";
192 if (is_array($filter)) {
194 if (count($filter) > 0) {
195 foreach ($filter as $key => $value) {
196 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.code') {
197 $sqlwhere[] = $this->db->sanitize($key).
" = ".((int) $value);
198 } elseif (strpos($key,
'date') !==
false) {
199 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->idate($value).
"'";
200 } elseif ($key ==
't.label') {
201 $sqlwhere[] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
203 $sqlwhere[] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($value).
"%'";
207 if (count($sqlwhere) > 0) {
208 $sql .=
" AND ".implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere);
218 $this->errors[] = $errormessage;
219 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
223 if (!empty($sortfield)) {
224 $sql .= $this->db->order($sortfield, $sortorder);
226 if (!empty($limit)) {
227 $sql .= $this->db->plimit($limit, $offset);
230 $resql = $this->db->query($sql);
232 $this->records = array();
233 $num = $this->db->num_rows($resql);
235 while ($obj = $this->db->fetch_object($resql)) {
236 $record =
new self($this->db);
238 $record->id = $obj->rowid;
239 $record->code = $obj->code;
240 $record->label = $obj->label;
241 $record->position = $obj->position;
242 $record->percent = $obj->percent;
243 $record->active = $obj->active;
244 $this->records[$record->id] = $record;
247 $this->db->free($resql);
249 return $this->records;
251 $this->errors[] =
'Error '.$this->db->lasterror();
252 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
266 public function update($user =
null, $notrigger = 0)
269 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
270 $sql .=
" code=".(isset($this->code) ? ((int) $this->code) :
"null").
",";
271 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape(trim($this->label)).
"'" :
"null").
",";
272 $sql .=
" position=".(isset($this->
position) ? (int) $this->
position :
"null").
",";
273 $sql .=
" percent=".(isset($this->label) ? (float) $this->percent :
"null").
",";
274 $sql .=
" active=".(isset($this->active) ? ((int) $this->active) :
"null");
275 $sql .=
" WHERE rowid=".(int) $this->
id;
279 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
280 $resql = $this->db->query($sql);
283 dol_syslog(get_class($this).
"::update Error ".$this->db->lasterror(), LOG_ERR);
284 $this->error =
"Error ".$this->db->lasterror();
285 $this->db->rollback();