public function save() { $q = new Query(); if ($id = (int) $this->getPrimaryKey()) { $update_list = array(); foreach ($this->_model_updated as $column => $null) { if (isset($this->_model_data[$column])) { $update_list[] = "{$column} = :" . strtoupper($column); } } if (count($update_list) > 0) { $update_str = join(',', $update_list); $q->q("UPDATE {$this->_table_name} SET {$update_str}\n WHERE {$this->_primary_key_field} = :ID")->bi('ID', $id); foreach ($this->_model_updated as $column => $null) { if (isset($this->_model_data[$column])) { $q->b(strtoupper($column), $this->_model_data[$column]); } } } else { // TODO Silent failing maybe bad? return $this; } } else { $field_list = array(); $value_list = array(); foreach ($this->_model_required as $column => $null) { if (!isset($this->_model_data[$column])) { throw new \Exception("Unable To Save {$this->_table_name} {$column} Is Required"); } } foreach ($this->_model_data as $column => $value) { if ($column == $this->getPrimaryKeyField()) { continue; } $field_list[] = $column; $value_list[] = ':' . strtoupper($column); } $field_str = join(',', $field_list); $value_str = join(',', $value_list); $q->q("INSERT INTO {$this->_table_name}\n ({$field_str})\n VALUES ({$value_str})"); foreach ($this->_model_data as $column => $value) { if ($column == $this->getPrimaryKeyField()) { continue; } $q->b(strtoupper($column), $this->_model_data[$column]); } } try { $r = $q->f(); if (!$this->getPrimaryKey()) { $this->_model_data[$this->_primary_key_field] = $r; } } catch (\Exception $e) { throw new \Exception("Unable To Save {$this->_table_name}", 500); } print_r($r); return $this; }