[Solar-svn] Revision 2761
pmjones at solarphp.com
pmjones at solarphp.com
Tue Sep 18 21:07:21 CDT 2007
branch: Solar_Sql_Model_Collection
* [ADD] Added minimal support for unit-of-work pattern; records that are deleted from the collection are not actually deleted from the database until you call save() on the collection itself.
* [ADD] Added _preSave() and _postSave() methods.
Modified: branches/orm/Solar/Sql/Model/Collection.php
===================================================================
--- branches/orm/Solar/Sql/Model/Collection.php 2007-09-19 01:54:39 UTC (rev 2760)
+++ branches/orm/Solar/Sql/Model/Collection.php 2007-09-19 02:07:20 UTC (rev 2761)
@@ -47,6 +47,15 @@
/**
*
+ * Unit-of-work records to delete.
+ *
+ * @var array
+ *
+ */
+ protected $_delete = array();
+
+ /**
+ *
* Magic getter. Disabled for collections.
*
* @param string $key The property name.
@@ -163,30 +172,49 @@
* Saves all the records from this collection to the database, inserting
* or updating as needed.
*
- * @param array $data An associative array of data to merge with existing
- * data in all records. Normally should not be set.
- *
* @return void
*
*/
- public function save($data = null)
+ public function save()
{
- foreach($this->_records as $key => $record) {
- $record->save($data);
+ // pre-logic
+ $this->_preSave();
+
+ // first, deletions
+ foreach ($this->_delete as $record) {
+ $status = $record->getStatus();
+ if ($status != 'new' && $status != 'deleted') {
+ $record->delete();
+ }
}
+
+ // next, saves
+ foreach($this->_data as $record) {
+ $status = $record->getStatus();
+ if ($status != 'deleted') {
+ $record->save();
+ }
+ }
+
+ // post-logic
+ $this->_postSave();
}
- /**
- *
- * Deletes all the records from this collection from the database.
- *
- * @return void
- *
- */
+ public function _preSave()
+ {
+ }
+
+ public function _postSave()
+ {
+ }
+
public function delete()
{
- foreach($this->_records as $key => $record) {
- $record->delete();
+ foreach ($this->_data as $record) {
+ $status = $record->getStatus();
+ if ($status != 'deleted') {
+ $record->delete();
+ }
}
}
@@ -282,4 +310,13 @@
return $this->_data[$key] = $val;
}
+
+ public function offsetUnset($key)
+ {
+ // keep the record for deletion
+ $this->_delete = $this->offsetGet($key);
+
+ // now remove from the data
+ parent::offsetUnset($key);
+ }
}
\ No newline at end of file
More information about the Solar-svn
mailing list