86 public function create($user, $notrigger = 0)
92 if (isset($this->code_region)) {
93 $this->code_region = trim($this->code_region);
95 if (isset($this->fk_pays)) {
96 $this->fk_pays = trim($this->fk_pays);
98 if (isset($this->nom)) {
99 $this->nom = trim($this->nom);
101 if (isset($this->cheflieu)) {
102 $this->cheflieu = trim($this->cheflieu);
104 if (isset($this->active)) {
105 $this->active = trim($this->active);
112 $sql =
"INSERT INTO ".$this->db->prefix().
"c_regions(";
114 $sql .=
"code_region,";
119 $sql .=
") VALUES (";
120 $sql .=
" ".(!isset($this->
rowid) ?
'NULL' :
"'".$this->db->escape($this->
rowid).
"'").
",";
121 $sql .=
" ".(!isset($this->code_region) ?
'NULL' :
"'".$this->db->escape($this->code_region).
"'").
",";
122 $sql .=
" ".(!isset($this->fk_pays) ?
'NULL' :
"'".$this->db->escape($this->fk_pays).
"'").
",";
123 $sql .=
" ".(!isset($this->
name) ?
'NULL' :
"'".$this->db->escape($this->
name).
"'").
",";
124 $sql .=
" ".(!isset($this->cheflieu) ?
'NULL' :
"'".$this->db->escape($this->cheflieu).
"'").
",";
125 $sql .=
" ".(!isset($this->active) ?
'NULL' :
"'".$this->db->escape($this->active).
"'");
130 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
131 $resql = $this->db->query($sql);
134 $this->errors[] =
"Error ".$this->db->lasterror();
138 $this->
id = $this->db->last_insert_id($this->db->prefix().
"c_regions");
143 foreach ($this->errors as $errmsg) {
144 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
145 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
147 $this->db->rollback();
164 public function fetch($id, $code_region =
'', $fk_pays =
'')
168 $sql .=
" t.code_region,";
169 $sql .=
" t.fk_pays,";
171 $sql .=
" t.cheflieu,";
173 $sql .=
" FROM ".$this->db->prefix().
"c_regions as t";
175 $sql .=
" WHERE t.rowid = ".((int) $id);
176 } elseif ($code_region) {
177 $sql .=
" WHERE t.code_region = '".$this->db->escape(strtoupper($code_region)).
"'";
178 } elseif ($fk_pays) {
179 $sql .=
" WHERE t.fk_pays = '".$this->db->escape(strtoupper($fk_pays)).
"'";
182 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
183 $resql = $this->db->query($sql);
185 if ($this->db->num_rows($resql)) {
186 $obj = $this->db->fetch_object($resql);
189 $this->
id = $obj->rowid;
190 $this->code_region = $obj->code_region;
191 $this->fk_pays = $obj->fk_pays;
192 $this->
name = $obj->nom;
193 $this->cheflieu = $obj->cheflieu;
194 $this->active = $obj->active;
197 $this->db->free($resql);
203 $this->error =
"Error ".$this->db->lasterror();
216 public function update($user =
null, $notrigger = 0)
218 global $conf, $langs;
222 if (isset($this->code_region)) {
223 $this->code_region = trim($this->code_region);
225 if (isset($this->fk_pays)) {
226 $this->fk_pays = trim($this->fk_pays);
228 if (isset($this->
name)) {
231 if (isset($this->cheflieu)) {
232 $this->cheflieu = trim($this->cheflieu);
234 if (isset($this->active)) {
235 $this->active = trim($this->active);
243 $sql =
"UPDATE ".$this->db->prefix().
"c_regions SET";
244 $sql .=
" code_region=".(isset($this->code_region) ?
"'".$this->db->escape($this->code_region).
"'" :
"null").
",";
245 $sql .=
" fk_pays=".(isset($this->fk_pays) ?
"'".$this->db->escape($this->fk_pays).
"'" :
"null").
",";
246 $sql .=
" nom=".(isset($this->
name) ?
"'".$this->db->escape($this->
name).
"'" :
"null").
",";
247 $sql .=
" cheflieu=".(isset($this->cheflieu) ?
"'".$this->db->escape($this->cheflieu).
"'" :
"null").
",";
248 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
249 $sql .=
" WHERE rowid=".((int) $this->
id);
253 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
254 $resql = $this->db->query($sql);
257 $this->errors[] =
"Error ".$this->db->lasterror();
262 foreach ($this->errors as $errmsg) {
263 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
264 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
266 $this->db->rollback();