public function save() { $connection = new dbconnection(); if ($this->id) { $sql = "update jabaianb." . get_class($this) . " set "; $set = array(); foreach ($this->data as $att => $value) { if ($att != 'id' && $value) { $set[] = "{$att} = '" . $value . "'"; } } $sql .= implode(",", $set); $sql .= " where id=" . $this->id; } else { $sql = "insert into jabaianb." . get_class($this) . " "; $sql .= "(" . implode(",", array_keys($this->data)) . ") "; $sql .= "values ('" . implode("','", array_values($this->data)) . "')"; } $connection->doExec($sql); /* * Sans cette modiffication, on recevait une erreur après un UPDATE: * Object not in prerequisite state: 7 ERROR: currval of sequence "post_id_seq" is not yet defined in this session */ if (!$this->id) { $this->id = $connection->getLastInsertId("jabaianb." . get_class($this)); } return $this->id == false ? NULL : $this->id; }
public function save() { $connection = new dbconnection(); if (isset($this->id)) { $sql = "update jabaianb." . get_class($this) . " set "; $set = array(); foreach ($this->data as $att => $value) { if ($att != 'id' && $value) { $set[] = "{$att} = '" . $value . "'"; } } $sql .= implode(",", $set); $sql .= " where id=" . $this->id; } else { $sql = "insert into jabaianb." . get_class($this) . " "; $sql .= "(" . implode(",", array_keys($this->data)) . ") "; $sql .= "values ('" . implode("','", array_values($this->data)) . "')"; } //print_r($sql); $connection->doExec($sql); $id = $connection->getLastInsertId("jabaianb." . get_class($this)); //print_r($id); return $id == false ? NULL : $id; }