[Solar-svn] Revision 3055
pmjones at solarphp.com
pmjones at solarphp.com
Thu Mar 27 09:00:56 CDT 2008
Solar_Sql_Model and Solar_Sql_Model_Record
Solar_Sql_Model_Record
----------------------
* [ADD] Property $_initial to track the initial values for table columns.
This helps us find out which columns have changed and which have not.
* [ADD] Method isChanged() to see if a column value has changed.
* [ADD] Method getChanged() to get a list of changed columns.
* [CHG] Method setStatus() now sets $_initial to the existing table-column
values when the status is set to inserted, updated, new, clean, etc. The
values are not set when the status is dirty or invalid.
Solar_Sql_Model
---------------
* [CHG] Method insert() now calls record setStatus('inserted') after insert.
* [CHG] Method update() now calls record setStatus('updated') after update.
* [CHG] Method newRecord() now calls record setStatus('clean') before
returning the record object.
Modified: trunk/Solar/Sql/Model/Record.php
===================================================================
--- trunk/Solar/Sql/Model/Record.php 2008-03-27 13:49:41 UTC (rev 3054)
+++ trunk/Solar/Sql/Model/Record.php 2008-03-27 14:00:56 UTC (rev 3055)
@@ -123,6 +123,19 @@
/**
*
+ * An array of the initial (clean) data for the record.
+ *
+ * This tracks only table-column data, not calculate-cols or related-cols.
+ *
+ * @var array
+ *
+ * @see setStatus()
+ *
+ */
+ protected $_initial = array();
+
+ /**
+ *
* Magic getter for record properties; automatically calls __getColName()
* methods when they exist.
*
@@ -182,7 +195,7 @@
// set to dirty only if not 'new'
if ($this->_status != 'new') {
- $this->_status = 'dirty';
+ $this->setStatus('dirty');
}
// if an accessor method exists, use it
@@ -625,11 +638,9 @@
try {
$this->_preInsert();
$this->_model->insert($this);
- $this->setStatus('inserted');
$this->_postInsert();
} catch (Solar_Sql_Adapter_Exception_QueryFailed $e) {
// failed at at the database for some reason
- $this->setStatus('invalid');
$this->setInvalid('*', $e->getInfo('pdo_text'));
throw $e;
}
@@ -671,11 +682,9 @@
$this->_preUpdate();
$where = null;
$this->_model->update($this, $where);
- $this->setStatus('updated');
$this->_postUpdate();
} catch (Solar_Sql_Adapter_Exception_QueryFailed $e) {
// failed at at the database for some reason
- $this->setStatus('invalid');
$this->setInvalid('*', $e->getInfo('pdo_text'));
throw $e;
}
@@ -810,7 +819,7 @@
$primary = $this->_model->primary_col;
$result = $this->_model->fetch($this->$primary);
$this->load($result);
- $this->_status = 'clean';
+ $this->setStatus('clean');
}
}
@@ -884,7 +893,7 @@
}
}
- $this->_status = 'invalid';
+ $this->setStatus('invalid');
$this->_invalid = $invalid;
throw $this->_exception('ERR_INVALID', array($this->_invalid));
}
@@ -931,7 +940,7 @@
*/
public function setInvalid($key, $message)
{
- $this->_status = 'invalid';
+ $this->setStatus('invalid');
$this->_invalid[$key][] = $message;
}
@@ -949,7 +958,7 @@
*/
public function setInvalids($list)
{
- $this->_status = 'invalid';
+ $this->setStatus('invalid');
foreach ($list as $key => $messages) {
foreach ((array) $messages as $message) {
$this->_invalid[$key][] = $message;
@@ -1044,6 +1053,16 @@
public function setStatus($status)
{
$this->_status = $status;
+ if ($this->_status != 'dirty' && $this->_status != 'invalid') {
+
+ // reset the initial data for table columns
+ foreach (array_keys($this->_model->table_cols) as $col) {
+ $this->_initial[$col] = $this->$col;
+ }
+
+ // can't be invalid, either
+ $this->_invalid = array();
+ }
}
/**
@@ -1061,6 +1080,43 @@
/**
*
+ * Tells if a particular table-column has changed.
+ *
+ * @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.
+ *
+ */
+ public function isChanged($col)
+ {
+ if (! array_key_exists($col, $this->_initial)) {
+ return null;
+ } else {
+ return $this->$col !== $this->_initial[$col];
+ }
+ }
+
+ /**
+ *
+ * Gets a list of all changed table columns.
+ *
+ * @return array
+ *
+ */
+ public function getChanged()
+ {
+ $list = array();
+ foreach ($this->_initial as $col => $val) {
+ if ($this->$col !== $val) {
+ $list[] = $col;
+ }
+ }
+ return $list;
+ }
+
+ /**
+ *
* Returns the exception (if any) generated by the most-recent call to the
* save() method.
*
Modified: trunk/Solar/Sql/Model.php
===================================================================
--- trunk/Solar/Sql/Model.php 2008-03-27 13:49:41 UTC (rev 3054)
+++ trunk/Solar/Sql/Model.php 2008-03-27 14:00:56 UTC (rev 3055)
@@ -1506,6 +1506,7 @@
$record = Solar::factory($class);
$record->setModel($this);
$record->load($data);
+ $record->setStatus('clean');
return $record;
}
@@ -1641,6 +1642,7 @@
// reload the data into the record if needed
if ($spec instanceof Solar_Sql_Model_Record) {
$spec->load($data);
+ $spec->setStatus('inserted');
}
// return the data as inserted
@@ -1730,6 +1732,7 @@
// reload the data into the record if needed
if ($spec instanceof Solar_Sql_Model_Record) {
$spec->load($data);
+ $spec->setStatus('updated');
}
// unserialize cols and return the data as updated
More information about the Solar-svn
mailing list