Example #1
0
 public function store()
 {
     if (!$this->fields_set()) {
         Logger::log("store: a required field is not set");
         return false;
     }
     // Check whether a row for this id exists
     $modelObj = new self();
     if (isset($this->id) && $modelObj->query_by_id($this->id)) {
         return $this->update();
     }
     // Not yet in database. Insert it
     $db = Database::instance();
     // Build statement
     // Note that we do not bind the table name nor the field names as
     // this seems to not be possible?
     $sql = "INSERT INTO " . self::get_table_name() . ' ' . $this->get_field_names() . " VALUES " . $this->get_bind_fields();
     // Build params
     $params = $this->get_field_values();
     print "sql: {$sql}. params: " . print_r($params, 1);
     try {
         $count = $db->manipulate($sql, $params, 1);
     } catch (Exception $e) {
         Logger::log("store: database failure: " . $e->getMessage());
         return false;
     }
     return $count === 1;
 }