public static function insert($table, array $params = array()) { $attributes = $params; $instance = new self(); $sql = "INSERT INTO " . $table . " ("; $sql .= join(", ", array_keys($attributes)); $sql .= ")VALUES("; $sql .= ":"; $sql .= join(", :", array_keys($attributes)); $sql .= ")"; try { $prep = $instance->prepare($sql); foreach ($attributes as $key => $value) { $prep->bindValue(':' . $key, $value); } if ($prep->execute()) { $instance->id = $instance->lastInsertId(); return true; } else { throw new \Exception("data could not be saved"); //return false; } } catch (\PDOException $e) { echo $e->getMessage(); } catch (\Exception $e) { echo $e->getMessage(); } }
public function prepare($sql) { $unions = $sql->getPart('unions'); $sql = $this->_renderSql($sql); $sql = str_replace($this->_hold, '?', $sql); if ($unions) { foreach ($unions as $union) { $b = new self($this->_db); $sql .= ' UNION (' . $b->prepare($union) . ')'; } } return $sql; }
public function delete() { $instance = new self(); $sql = "DELETE FROM " . static::$table . " WHERE id=" . $this->id; $prep = $instance->prepare($sql); try { if ($prep->execute()) { return true; } else { return false; } } catch (\Exception $e) { echo $e->getMessage(); } catch (\PDOException $e) { echo $e->getMessage(); } }