Esempio n. 1
0
 /**
  * Code below was lifted from Solar_Sql_Model_Record::isChanged($col=null)
  * 
  * Tells if a particular table-column has changed.
  * 
  * This is slightly complicated.  Changes to or from a null are reported
  * as "changed".  If both the initial value and new value are numeric
  * (that is, whether they are string/float/int), they are compared using
  * normal inequality (!=).  Otherwise, the initial value and new value
  * are compared using strict inequality (!==).
  * 
  * This complexity results from converting string and numeric values in
  * and out of the database.  Coming from the database, a string numeric
  * '1' might be filtered to an integer 1 at some point, making it look
  * like the value was changed when in practice it has not.
  * 
  * Similarly, we need to make allowances for nulls, because a non-numeric
  * null is loosely equal to zero or an empty string.
  * 
  * @param string $col The table-column name.
  * 
  * @return void|bool Returns null if the table-column name does not exist,
  * boolean true if the data is changed, boolean false if not changed.
  * 
  * @todo How to handle changes to array values?
  * 
  */
 public function isChanged($col = null)
 {
     // if no column specified, check if the record as a whole has changed
     if ($col === null) {
         $cols = $this->_model->getTableColNames();
         foreach ($cols as $col) {
             if ($this->isChanged($col)) {
                 return true;
             }
         }
         return false;
     }
     // col needs to exist in the initial array
     if (!array_key_exists($col, $this->_initial_data) && array_key_exists($col, $this->_data) || array_key_exists($col, $this->_initial_data) && !array_key_exists($col, $this->_data)) {
         return true;
     } else {
         if (!array_key_exists($col, $this->_initial_data) && !array_key_exists($col, $this->_data)) {
             return null;
         } else {
             // array_key_exists($col, $this->_initial_data)
             // && array_key_exists($col, $this->_data)
             // track changes to or from null
             $from_null = $this->_initial_data[$col] === null && $this->_data[$col] !== null;
             $to_null = $this->_initial_data[$col] !== null && $this->_data[$col] === null;
             if ($from_null || $to_null) {
                 return true;
             }
             // track numeric changes
             $both_numeric = is_numeric($this->_initial_data[$col]) && is_numeric($this->_data[$col]);
             if ($both_numeric) {
                 // use normal inequality
                 return $this->_initial_data[$col] != (string) $this->_data[$col];
             }
             // use strict inequality
             return $this->_initial_data[$col] !== $this->_data[$col];
         }
     }
 }