diff --git a/etemplate/inc/class.so_sql.inc.php b/etemplate/inc/class.so_sql.inc.php index caee32d4b8..b7b0c15289 100644 --- a/etemplate/inc/class.so_sql.inc.php +++ b/etemplate/inc/class.so_sql.inc.php @@ -23,7 +23,7 @@ * @package etemplate * @subpackage contrib * @author RalfBecker-AT-outdoor-training.de - * @license GPL + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License */ class so_sql { @@ -155,6 +155,8 @@ class so_sql */ function data_merge($new) { + if ($this->debug) echo "
so_sql::data_merge(".print_r($new).")
\n"; + if (!is_array($new) || !count($new)) { return; @@ -173,6 +175,7 @@ class so_sql $this->data[$col] = $new[$col]; } } + if ($this->debug) _debug_array($this->data); } /** @@ -307,13 +310,15 @@ class so_sql $this->data2db(); + //echo "so_sql::save(".print_r($keys,true).") autoinc_id='$this->autoinc_id', data="; _debug_array($this->data); + if ($this->autoinc_id && !$this->data[$this->db_key_cols[$this->autoinc_id]]) // insert { foreach($this->db_cols as $db_col => $col) { if (!$this->autoinc_id || $db_col != $this->autoinc_id) // not write auto-inc-id { - $data[$db_col] = $this->data[$col] == '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; + $data[$db_col] = (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; } } $this->db->insert($this->table_name,$data,false,__LINE__,__FILE__); @@ -327,18 +332,25 @@ class so_sql { foreach($this->db_data_cols as $db_col => $col) { - $data[$db_col] = $this->data[$col] == '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; + $data[$db_col] = (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; } $keys = ''; foreach($this->db_key_cols as $db_col => $col) { $keys[$db_col] = $this->data[$col]; } - $this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__); + if (!$this->autoinc_id) // always try an insert if we have no autoinc_id, as we dont know if the data exists + { + $this->db->insert($this->table_name,$data,$keys,__LINE__,__FILE__); + } + else + { + $this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__); + } } $this->db2data(); - return $this->db->errno; + return $this->db->Errno; } /** @@ -390,9 +402,11 @@ class so_sql * @param string $op defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together * @param int/boolean $start if != false, return only maxmatch rows begining with start * @param array $filter if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards + * @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or + * "LEFT JOIN table2 ON (x=y)", Note: there's no quoting done on $join! * @return array of matching rows (the row is an array of the cols) or False */ - function search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null) + function search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='') { if (!is_array($criteria)) { @@ -403,17 +417,23 @@ class so_sql $criteria = $this->data2db($criteria); foreach($this->db_cols as $db_col => $col) - { //echo "testing col='$col', criteria[$col]='".$criteria[$col]."'so_sql::search(,only_keys=$only_keys,order_by='$order_by',wildcard='$wildcard',empty=$empty,$op,start='$start',".print_r($filter,true).") query=".print_r($query,true).", total='$this->total'
\n"; echo "