69 public function create($user, $notrigger = 0)
75 if (isset($this->code_region)) {
76 $this->code_region = trim($this->code_region);
78 if (isset($this->fk_pays)) {
79 $this->fk_pays = trim($this->fk_pays);
81 if (isset($this->nom)) {
82 $this->nom = trim($this->nom);
84 if (isset($this->cheflieu)) {
85 $this->cheflieu = trim($this->cheflieu);
87 if (isset($this->active)) {
88 $this->active = trim($this->active);
95 $sql =
"INSERT INTO ".$this->db->prefix().
"c_regions(";
97 $sql .=
"code_region,";
102 $sql .=
") VALUES (";
103 $sql .=
" ".(!isset($this->
rowid) ?
'NULL' :
"'".$this->db->escape($this->
rowid).
"'").
",";
104 $sql .=
" ".(!isset($this->code_region) ?
'NULL' :
"'".$this->db->escape($this->code_region).
"'").
",";
105 $sql .=
" ".(!isset($this->fk_pays) ?
'NULL' :
"'".$this->db->escape($this->fk_pays).
"'").
",";
106 $sql .=
" ".(!isset($this->
name) ?
'NULL' :
"'".$this->db->escape($this->
name).
"'").
",";
107 $sql .=
" ".(!isset($this->cheflieu) ?
'NULL' :
"'".$this->db->escape($this->cheflieu).
"'").
",";
108 $sql .=
" ".(!isset($this->active) ?
'NULL' :
"'".$this->db->escape($this->active).
"'");
113 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
114 $resql = $this->db->query($sql);
117 $this->errors[] =
"Error ".$this->db->lasterror();
121 $this->
id = $this->db->last_insert_id($this->db->prefix().
"c_regions");
126 foreach ($this->errors as $errmsg) {
127 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
128 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
130 $this->db->rollback();
147 public function fetch($id, $code_region =
'', $fk_pays =
'')
151 $sql .=
" t.code_region,";
152 $sql .=
" t.fk_pays,";
154 $sql .=
" t.cheflieu,";
156 $sql .=
" FROM ".$this->db->prefix().
"c_regions as t";
158 $sql .=
" WHERE t.rowid = ".((int) $id);
159 } elseif ($code_region) {
160 $sql .=
" WHERE t.code_region = '".$this->db->escape(strtoupper($code_region)).
"'";
161 } elseif ($fk_pays) {
162 $sql .=
" WHERE t.fk_pays = '".$this->db->escape(strtoupper($fk_pays)).
"'";
165 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
166 $resql = $this->db->query($sql);
168 if ($this->db->num_rows($resql)) {
169 $obj = $this->db->fetch_object($resql);
172 $this->
id = $obj->rowid;
173 $this->code_region = $obj->code_region;
174 $this->fk_pays = $obj->fk_pays;
175 $this->
name = $obj->nom;
176 $this->cheflieu = $obj->cheflieu;
177 $this->active = $obj->active;
180 $this->db->free($resql);
186 $this->error =
"Error ".$this->db->lasterror();
199 public function update($user =
null, $notrigger = 0)
201 global $conf, $langs;
205 if (isset($this->code_region)) {
206 $this->code_region = trim($this->code_region);
208 if (isset($this->fk_pays)) {
209 $this->fk_pays = trim($this->fk_pays);
211 if (isset($this->
name)) {
214 if (isset($this->cheflieu)) {
215 $this->cheflieu = trim($this->cheflieu);
217 if (isset($this->active)) {
218 $this->active = trim($this->active);
226 $sql =
"UPDATE ".$this->db->prefix().
"c_regions SET";
227 $sql .=
" code_region=".(isset($this->code_region) ?
"'".$this->db->escape($this->code_region).
"'" :
"null").
",";
228 $sql .=
" fk_pays=".(isset($this->fk_pays) ?
"'".$this->db->escape($this->fk_pays).
"'" :
"null").
",";
229 $sql .=
" nom=".(isset($this->
name) ?
"'".$this->db->escape($this->
name).
"'" :
"null").
",";
230 $sql .=
" cheflieu=".(isset($this->cheflieu) ?
"'".$this->db->escape($this->cheflieu).
"'" :
"null").
",";
231 $sql .=
" active=".(isset($this->active) ? $this->active :
"null");
232 $sql .=
" WHERE rowid=".((int) $this->
id);
236 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
237 $resql = $this->db->query($sql);
240 $this->errors[] =
"Error ".$this->db->lasterror();
245 foreach ($this->errors as $errmsg) {
246 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
247 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
249 $this->db->rollback();