[Solar-svn] Revision 2767
pmjones at solarphp.com
pmjones at solarphp.com
Fri Sep 21 11:49:40 CDT 2007
branch: Model, Collection, and Record changes
Solar_Sql_Model
---------------
* [CHG] Method newCollection() no longer requires the $data parameter, which
means you can now get a new empty collection if you want one.
Solar_Sql_Model_Collection
--------------------------
* [CHG] Methods __get(), __set(), and __unset() are now available.
* [DEL] Removed initial attempt at unit-of-work support.
* [ADD] Added toArray() method.
Solar_Sql_Model_Record
----------------------
* [CHG] Method setModel() now looks at "calculate cols" when defining accessor
methods.
* [CHG] Because the new Solar_Struct uses "unified" __get/offsetGet() logic,
changed toArray() method to avoid infinite recursive SQL hits to populate
properties representing related records/collections.
* [FIX] Added missing _preDelete() and _postDelete() methods.
Modified: branches/orm/Solar/Sql/Model/Collection.php
===================================================================
--- branches/orm/Solar/Sql/Model/Collection.php 2007-09-21 16:38:58 UTC (rev 2766)
+++ branches/orm/Solar/Sql/Model/Collection.php 2007-09-21 16:49:39 UTC (rev 2767)
@@ -47,65 +47,6 @@
/**
*
- * Unit-of-work records to delete.
- *
- * @var array
- *
- */
- protected $_delete = array();
-
- /**
- *
- * Magic getter. Disabled for collections.
- *
- * @param string $key The property name.
- *
- * @return void
- *
- */
- public function __get($key)
- {
- throw $this->_exception(
- 'ERR_PROPERTY_NOT_DEFINED',
- array('property' => "\$$key")
- );
- }
-
- /**
- *
- * Magic setter. Disabled for collections.
- *
- * @param string $key The property name.
- *
- * @param mixed $val The property value.
- *
- * @return void
- *
- */
- public function __set($key, $val)
- {
- throw $this->_exception(
- 'ERR_PROPERTY_NOT_DEFINED',
- array('property' => "\$$key")
- );
- }
-
- /**
- *
- * Sets a key in the data to null.
- *
- * @param string $key The requested data key.
- *
- * @return void
- *
- */
- public function __unset($key)
- {
- $this->__set($key, null);
- }
-
- /**
- *
* Injects the model from which the data originates.
*
* Also loads accessor method lists for column and related properties.
@@ -167,6 +108,16 @@
}
}
+ public function toArray()
+ {
+ $data = array();
+ $clone = clone($this);
+ foreach ($clone as $key => $record) {
+ $data[$key] = $record->toArray();
+ }
+ return $data;
+ }
+
/**
*
* Saves all the records from this collection to the database, inserting
@@ -180,14 +131,6 @@
// 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();
@@ -310,13 +253,4 @@
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
Modified: branches/orm/Solar/Sql/Model/Record.php
===================================================================
--- branches/orm/Solar/Sql/Model/Record.php 2007-09-21 16:38:58 UTC (rev 2766)
+++ branches/orm/Solar/Sql/Model/Record.php 2007-09-21 16:49:39 UTC (rev 2767)
@@ -118,7 +118,7 @@
// do we need to load relationship data?
$load_related = empty($this->_data[$key]) &&
- array_key_exists($key, $this->_model->related);
+ ! empty($this->_model->related[$key]);
if ($load_related) {
// the key was for a relation that has no data yet.
@@ -126,26 +126,20 @@
$this->_data[$key] = $this->_model->fetchRelatedObject(
$this,
$key,
- $this->related_page[$key]
+ $this->_related_page[$key]
);
}
// default value
$result = null;
- // if an accessor method exists, use it.
+ // if an accessor method exists, use it to return the data
if (! empty($this->_access_methods['get'][$key])) {
$method = $this->_access_methods['get'][$key];
- $result = $this->$method();
+ return $this->$method();
+ } else {
+ return $this->_data[$key];
}
-
- // look for the data key and return its value.
- if (array_key_exists($key, $this->_data)) {
- $result = $this->_data[$key];
- }
-
- // done
- return $result;
}
/**
@@ -285,8 +279,9 @@
// it's not enough to just merge the data. although slower, we need
// to loop so that __set() is honored.
$this->_model->unserializeCols($data);
+
foreach ($data as $key => $val) {
- $this->$key = $val;
+ $this->__set($key, $val);
}
// load related data as records and collections
@@ -347,7 +342,8 @@
// get a list of table-column and related-data properties names
$vars = array_merge(
array_keys($this->_model->table_cols),
- array_keys($this->_model->related)
+ array_keys($this->_model->related),
+ (array) $this->_model->calculate_cols
);
// look for access methods on each one
@@ -405,29 +401,39 @@
*/
public function toArray()
{
- // work with a clone so we don't screw up the iterator.
- $clone = clone $this;
+ $data = array();
+ $keys = array_keys($this->_data);
- // get a copy of each data element
- $data = array();
- foreach ($clone as $key => $val) {
- // get the sub-value
- if ($val instanceof Solar_Sql_Model_Record) {
- $sub = $val->toArray();
+ foreach ($keys as $key) {
+
+ // is the key a related record/collection, but not fetched yet?
+ $empty_related = ! empty($this->_model->related[$key]) &&
+ empty($this->_data[$key]);
+
+ if ($empty_related) {
+
+ // do not fetch related just for array convertion, this leads
+ // to some deep (perhaps infinite?) recurstion
+ if ($this->_model->related[$key]['type'] == 'has_many') {
+ $val = array();
+ } else {
+ $val = null;
+ }
+
} else {
- $sub = $val;
+
+ // not an empty-related. get the existing value.
+ $val = $this->$key;
+
+ // get the sub-value if any
+ if ($val instanceof Solar_Sql_Model_Record ||
+ $val instanceof Solar_Sql_Model_Collection) {
+ $val = $val->toArray();
+ }
}
- // if the sub-value is a has-many but is empty, convert to an
- // array
- if (empty($sub) && ! empty($this->_model->related[$key]) &&
- $this->_model->related[$key]['type'] == 'has_many') {
- // convert from null to array
- $sub = array();
- }
-
// keep the sub-value
- $data[$key] = $sub;
+ $data[$key] = $val;
}
// done!
@@ -574,6 +580,14 @@
{
}
+ protected function _preDelete()
+ {
+ }
+
+ protected function _postDelete()
+ {
+ }
+
protected function _preFilter()
{
}
@@ -826,15 +840,19 @@
$form->setValues($this->toArray(), $array_name);
$form->addInvalids($this->_invalid, $array_name);
- // now: how to set form status and form messages?
- if ($this->_status == 'invalid') {
+ // set the form status
+ switch (true) {
+ case $this->_status == 'invalid':
$form->setStatus(false);
- }
-
- if ($this->_status == 'inserted' || $this->_status == 'updated') {
+ break;
+ case $this->_status == 'inserted' || $this->_status == 'updated':
$form->setStatus(true);
+ break;
}
+ // @todo: add invalidation messages to the form itself, where
+ // elements are missing.
+
return $form;
}
}
\ No newline at end of file
Modified: branches/orm/Solar/Sql/Model.php
===================================================================
--- branches/orm/Solar/Sql/Model.php 2007-09-21 16:38:58 UTC (rev 2766)
+++ branches/orm/Solar/Sql/Model.php 2007-09-21 16:49:39 UTC (rev 2767)
@@ -1569,12 +1569,12 @@
*
* Returns the appropriate collection object for this model.
*
- * @param array $data The data to load into the record.
+ * @param array $data The data to load into the collection, if any.
*
* @return Solar_Sql_Model_Collection A collection object.
*
*/
- public function newCollection($data)
+ public function newCollection($data = null)
{
// the model class we'll use for the collection
$class = $this->_stack->load('Collection', false);
More information about the Solar-svn
mailing list