summaryrefslogtreecommitdiff
path: root/idiorm.php
diff options
context:
space:
mode:
authorJamie Matthews <[email protected]>2010-02-09 22:36:12 +0000
committerJamie Matthews <[email protected]>2010-02-09 22:36:12 +0000
commit59f4bdae494589dded0dcbac8eae1ef3f5f82a94 (patch)
tree3b0c5c751bd985de3feff1e20814c27f6791905f /idiorm.php
parent8769a0bbdbfd3cd908b556f5d1d0c44301e3041f (diff)
Added save functionality
Diffstat (limited to 'idiorm.php')
-rw-r--r--idiorm.php64
1 files changed, 60 insertions, 4 deletions
diff --git a/idiorm.php b/idiorm.php
index 01f6ab5..f0d5b38 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -39,6 +39,7 @@
private $where = array();
private $data = array();
+ private $dirty_fields = array();
// Are we updating or inserting?
private $update_or_insert = self::UPDATE;
@@ -78,7 +79,7 @@
$this->data = $data;
}
- public function create($data) {
+ public function create() {
$this->update_or_insert = self::INSERT;
return $this;
}
@@ -127,7 +128,7 @@
$where[self::OPERATOR],
'?'
));
- $this->values[] = $first[self::VALUE];
+ $this->values[] = $where[self::VALUE];
}
}
@@ -141,8 +142,7 @@
if ($this->find_type == self::FIND_ONE) {
$result = $statement->fetch(PDO::FETCH_ASSOC);
- $this->data = $result;
- return $this;
+ return $result ? self::hydrate($this->table_name, $result) : $result;
} else {
$instances = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
@@ -162,5 +162,61 @@
}
return vsprintf($sql, $quoted_values);
}
+
+ public function get($key) {
+ return isset($this->data[$key]) ? $this->data[$key] : null;
+ }
+
+ private function get_id_column_name() {
+ if (isset(self::$config['id_column_overrides'][$this->table_name])) {
+ return self::$config['id_column_overrides'][$this->table_name];
+ } else {
+ return self::$config['id_column'];
+ }
+ }
+
+ public function id() {
+ return $this->get($this->get_id_column_name());
+ }
+
+ public function set($key, $value) {
+ $this->data[$key] = $value;
+ $this->dirty_fields[$key] = $value;
+ }
+
+ public function save() {
+ $query = array();
+ $values = array_values($this->dirty_fields);
+
+ if ($this->update_or_insert == self::UPDATE) {
+ $query[] = "UPDATE";
+ $query[] = $this->table_name;
+ $query[] = "SET";
+ $field_list = array();
+ foreach ($this->dirty_fields as $key => $value) {
+ $field_list[] = "$key = ?";
+ }
+ $query[] = join(", ", $field_list);
+ $query[] = "WHERE";
+ $query[] = $this->get_id_column_name();
+ $query[] = "= ?";
+ $values[] = $this->id();
+ } else {
+ $query[] = "INSERT INTO";
+ $query[] = $this->table_name;
+ $query[] = "(" . join(", ", array_keys($this->dirty_fields)) . ")";
+ $query[] = "VALUES";
+ $placeholders = array();
+ for ($i=0; $i<count($this->dirty_fields); $i++) {
+ $placeholders[] = "?";
+ }
+ $query[] = "(" . join(", ", $placeholders) . ")";
+
+ }
+
+ $query = join(" ", $query);
+ $statement = self::$db->prepare($query);
+ $statement->execute($values);
+ }
}