Beispiel #1
0
 /**
 		Insert/update DB record
 			@public
 	**/
 public function save()
 {
     if ($this->empty) {
         // Axon is empty
         trigger_error(self::TEXT_AxonEmpty);
         return;
     }
     if (method_exists($this, 'beforeSave')) {
         // Execute beforeSave event
         $this->beforeSave();
     }
     $_new = TRUE;
     if ($this->keys) {
         // If ALL primary keys are NULL, this is a new record
         foreach ($this->keys as $_value) {
             if (!is_null($_value)) {
                 $_new = FALSE;
                 break;
             }
         }
     }
     if ($_new) {
         // Insert new record
         $_fields = '';
         $_values = '';
         foreach ($this->fields as $_field => $_value) {
             $_fields .= ($_fields ? ',' : '') . $_field;
             $_values .= ($_values ? ',' : '') . ':' . $_field;
             $_bind[':' . $_field] = array($_value, SQLdb::type($_value));
         }
         F3::sqlBind('INSERT INTO ' . $this->table . ' (' . $_fields . ') ' . 'VALUES (' . $_values . ');', $_bind, $this->db);
     } else {
         // Update record
         $_set = '';
         foreach ($this->fields as $_field => $_value) {
             $_set .= ($_set ? ',' : '') . ($_field . '=:' . $_field);
             $_bind[':' . $_field] = array($_value, SQLdb::type($_value));
         }
         // Use prior primary key values (if changed) to find record
         $_cond = '';
         foreach ($this->keys as $_key => $_value) {
             $_cond .= ($_cond ? ' AND ' : '') . ($_key . '=:c_' . $_key);
             $_bind[':c_' . $_key] = array($_value, SQLdb::type($_value));
         }
         F3::sqlBind('UPDATE ' . $this->table . ' SET ' . $_set . (is_null($_cond) ? '' : ' WHERE ' . $_cond) . ';', $_bind, $this->db);
     }
     if ($this->keys) {
         // Update primary keys with new values
         foreach (array_keys($this->keys) as $_field) {
             $this->keys[$_field] = $this->fields[$_field];
         }
     }
     if (method_exists($this, 'afterSave')) {
         // Execute afterSave event
         $this->afterSave();
     }
 }