Пример #1
0
 /**
  * standard set* implementation.
  *
  * takes data and uses it to set dates/strings etc.
  * normally called from __call..  
  *
  * Current supports
  *   date      = using (standard time format, or unixtimestamp).... so you could create a method :
  *               function setLastread($string) { $this->fromValue('lastread',strtotime($string)); }
  *
  *   time      = using strtotime 
  *   datetime  = using  same as date - accepts iso standard or unixtimestamp.
  *   string    = typecast only..
  * 
  * TODO: add formater:: eg. d/m/Y for date! ???
  *
  * @param   string       column of database
  * @param   mixed        value to assign
  *
  * @return   true| false     (False on error)
  * @access   public 
  * @see      DB_DataObject::_call
  */
 function fromValue($col, $value)
 {
     global $_DB_DATAOBJECT;
     $options = $_DB_DATAOBJECT['CONFIG'];
     $cols = $this->table();
     // dont know anything about this col..
     if (!isset($cols[$col])) {
         $this->{$col} = $value;
         return true;
     }
     //echo "FROM VALUE $col, {$cols[$col]}, $value\n";
     switch (true) {
         // set to null and column is can be null...
         case !($cols[$col] & DB_DATAOBJECT_NOTNULL) && DB_DataObject::_is_null($value, false):
         case is_object($value) && is_a($value, 'DB_DataObject_Cast'):
             $this->{$col} = $value;
             return true;
             // fail on setting null on a not null field..
         // fail on setting null on a not null field..
         case $cols[$col] & DB_DATAOBJECT_NOTNULL && DB_DataObject::_is_null($value, false):
             return false;
         case $cols[$col] & DB_DATAOBJECT_DATE && $cols[$col] & DB_DATAOBJECT_TIME:
             // empty values get set to '' (which is inserted/updated as NULl
             if (!$value) {
                 $this->{$col} = '';
             }
             if (is_numeric($value)) {
                 $this->{$col} = date('Y-m-d H:i:s', $value);
                 return true;
             }
             // eak... - no way to validate date time otherwise...
             $this->{$col} = (string) $value;
             return true;
         case $cols[$col] & DB_DATAOBJECT_DATE:
             // empty values get set to '' (which is inserted/updated as NULl
             if (!$value) {
                 $this->{$col} = '';
                 return true;
             }
             if (is_numeric($value)) {
                 $this->{$col} = date('Y-m-d', $value);
                 return true;
             }
             // try date!!!!
             require_once 'Date.php';
             $x = new Date($value);
             $this->{$col} = $x->format("%Y-%m-%d");
             return true;
         case $cols[$col] & DB_DATAOBJECT_TIME:
             // empty values get set to '' (which is inserted/updated as NULl
             if (!$value) {
                 $this->{$col} = '';
             }
             $guess = strtotime($value);
             if ($guess != -1) {
                 $this->{$col} = date('H:i:s', $guess);
                 return $return = true;
             }
             // otherwise an error in type...
             return false;
         case $cols[$col] & DB_DATAOBJECT_STR:
             $this->{$col} = (string) $value;
             return true;
             // todo : floats numerics and ints...
         // todo : floats numerics and ints...
         default:
             $this->{$col} = $value;
             return true;
     }
 }
Пример #2
0
 /**
  * Updates  current objects variables into the database
  * DB_DataObject fix to allow update of objects that were fetched from a join query.
  *
  */
 function _update($dataObject = false)
 {
     global $_DB_DATAOBJECT;
     // connect will load the config!
     $this->_connect();
     $original_query = $this->_query;
     $items = isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) ? $_DB_DATAOBJECT['INI'][$this->_database][$this->__table] : $this->table();
     // only apply update against sequence key if it is set?????
     $seq = $this->sequenceKey();
     if ($seq[0] !== false) {
         $keys = array($seq[0]);
         if (!isset($this->{$keys[0]}) && $dataObject !== true) {
             $this->raiseError("update: trying to perform an update without\n                        the key set, and argument to update is not\n                        DB_DATAOBJECT_WHEREADD_ONLY\n                    ", DB_DATAOBJECT_ERROR_INVALIDARGS);
             return false;
         }
     } else {
         $keys = $this->keys();
     }
     $pkName = $keys[0];
     $pkVal = $this->{$keys[0]};
     if (!$items) {
         $this->raiseError("update:No table definition for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG);
         return false;
     }
     $datasaved = 1;
     $settings = '';
     $this->_connect();
     $DB =& $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
     $dbtype = $DB->dsn["phptype"];
     $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
     $options = $_DB_DATAOBJECT['CONFIG'];
     $ignore_null = !isset($options['disable_null_strings']) || !is_string($options['disable_null_strings']) || strtolower($options['disable_null_strings']) !== 'full';
     foreach ($items as $k => $v) {
         if (!isset($this->{$k}) && $ignore_null) {
             continue;
         }
         // ignore stuff thats
         // dont write things that havent changed..
         if ($dataObject !== false && isset($dataObject->{$k}) && $dataObject->{$k} === $this->{$k}) {
             continue;
         }
         // - dont write keys to left.!!!
         if (in_array($k, $keys)) {
             continue;
         }
         // dont insert data into mysql timestamps
         // use query() if you really want to do this!!!!
         if ($v & DB_DATAOBJECT_MYSQLTIMESTAMP) {
             continue;
         }
         if ($settings) {
             $settings .= ', ';
         }
         $kSql = $quoteIdentifiers ? $DB->quoteIdentifier($k) : $k;
         if (is_a($this->{$k}, 'DB_DataObject_Cast')) {
             $value = $this->{$k}->toString($v, $DB);
             if (PEAR::isError($value)) {
                 $this->raiseError($value->getMessage(), DB_DATAOBJECT_ERROR_INVALIDARG);
                 return false;
             }
             $settings .= "{$kSql} = {$value} ";
             continue;
         }
         // special values ... at least null is handled...
         if (!($v & DB_DATAOBJECT_NOTNULL) && DB_DataObject::_is_null($this, $k)) {
             $settings .= "{$kSql} = NULL ";
             continue;
         }
         // DATE is empty... on a col. that can be null..
         // note: this may be usefull for time as well..
         if (!$this->{$k} && ($v & DB_DATAOBJECT_DATE || $v & DB_DATAOBJECT_TIME) && !($v & DB_DATAOBJECT_NOTNULL)) {
             $settings .= "{$kSql} = NULL ";
             continue;
         }
         if ($v & DB_DATAOBJECT_STR) {
             $settings .= "{$kSql} = " . $this->_quote((string) ($v & DB_DATAOBJECT_BOOL ? $this->{$k} === 'f' ? 0 : (int) (bool) $this->{$k} : $this->{$k})) . ' ';
             continue;
         }
         if (is_numeric($this->{$k})) {
             $settings .= "{$kSql} = {$this->{$k}} ";
             continue;
         }
         // at present we only cast to integers
         // - V2 may store additional data about float/int
         $settings .= "{$kSql} = " . intval($this->{$k}) . ' ';
     }
     if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
         $this->debug("got keys as " . serialize($keys), 3);
     }
     if ($dataObject !== true) {
         $this->_build_condition($items, $keys);
     } else {
         // prevent wiping out of data!
         if (empty($this->_query['condition'])) {
             $this->raiseError("update: global table update not available\n                        do \$do->whereAdd('1=1'); if you really want to do that.\n                    ", DB_DATAOBJECT_ERROR_INVALIDARGS);
             return false;
         }
     }
     if ($settings && isset($this->_query) && $this->_query['condition']) {
         $table = $quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table;
         if ($dataObject === DB_DATAOBJECT_WHEREADD_ONLY) {
             $r = $this->_query("UPDATE  {$table}  SET {$settings} {$this->_query['condition']} ");
         } else {
             $pkName = $quoteIdentifiers ? $DB->quoteIdentifier($pkName) : $pkName;
             $pkVal = $DB->quote($pkVal);
             $r = $this->_query("UPDATE  {$table}  SET {$settings} WHERE {$pkName} = {$pkVal}");
         }
         // restore original query conditions.
         $this->_query = $original_query;
         if (PEAR::isError($r)) {
             $this->raiseError($r);
             return false;
         }
         if ($r < 1) {
             return 0;
         }
         $this->_clear_cache();
         return $r;
     }
     // restore original query conditions.
     $this->_query = $original_query;
     // if you manually specified a dataobject, and there where no changes - then it's ok..
     if ($dataObject !== false) {
         return true;
     }
     $this->raiseError("update: No Data specifed for query {$settings} , {$this->_query['condition']}", DB_DATAOBJECT_ERROR_NODATA);
     return false;
 }