[Solar-svn] Revision 2746
pmjones at solarphp.com
pmjones at solarphp.com
Sat Sep 8 11:45:19 CDT 2007
branch: Solar_Sql_Model changes
Solar_Sql_Model
---------------
* [FIX] Re-added the 'bind' param for fetchOne(), fetchAll(), etc. At one point I thought this was causing trouble with has-many related records, but apparently no more.
* [ADD] Method fetchValue() to fetch a single value from the model.
* [CHG] Methods fetchRelatedArray() and fetchRelatedObject() now take a 4th param, $bind, for values to bind into the query.
* [CHG] Method insertRecord(), on success, now sets the record status as 'inserted' (vice 'clean').
* [CHG] Method updateRecord(), on success, now sets the record status as 'updated' (vice 'clean').
Solar_Sql_Model_Record
----------------------
* [CHG] Method load() now takes a second param, $cols, a whitelist of columns to load. Columns not in this list will not be loaded. If null, all columns from the data are loaded.
* [FIX] Method toArray() now works on a clone of $this, to avoid iterator problems.
* [ADD] Added method form() to return a Solar_Form object pre-populated with elements, values, invalidation messages, and success/failure status, based on the record.
Solar_Sql_Model_Filter_ValidateUnique
-------------------------------------
* [FIX] Now binds the record values into the query.
Modified: branches/orm/Solar/Sql/Model/Filter/ValidateUnique.php
===================================================================
--- branches/orm/Solar/Sql/Model/Filter/ValidateUnique.php 2007-09-08 16:37:42 UTC (rev 2745)
+++ branches/orm/Solar/Sql/Model/Filter/ValidateUnique.php 2007-09-08 16:45:19 UTC (rev 2746)
@@ -43,13 +43,13 @@
// see if we can fetch a row, with only the primary-key column to
// reduce resource usage.
- $row = $this->_filter->model->fetchOne(array(
+ $result = $this->_filter->model->fetchValue(array(
'where' => $where,
'cols' => array($primary),
- 'bind' => $this->_filter->record->toArray(),
+ 'bind' => $this->_filter->getData()->toArray(),
));
- // if empty, no row was returned, so the value is unique.
- return empty($row);
+ // if empty, no result was returned, so the value is unique.
+ return empty($result);
}
}
\ No newline at end of file
Modified: branches/orm/Solar/Sql/Model/Record.php
===================================================================
--- branches/orm/Solar/Sql/Model/Record.php 2007-09-08 16:37:42 UTC (rev 2745)
+++ branches/orm/Solar/Sql/Model/Record.php 2007-09-08 16:45:19 UTC (rev 2746)
@@ -244,11 +244,23 @@
*
* @param array|Solar_Struct $spec The data to load into the object.
*
+ * @param array $cols Load only these columns.
+ *
* @return void
*
*/
- public function load($spec)
+ public function load($spec, $cols = null)
{
+ // remove columns not in the whitelist
+ if (! empty($cols)) {
+ $cols = (array) $cols;
+ foreach ($spec as $key => $val) {
+ if (! in_array($key, $cols)) {
+ unset($spec[$key]);
+ }
+ }
+ }
+
// do the "real" load
parent::load($spec);
@@ -381,9 +393,12 @@
*/
public function toArray()
{
- // get a copy of everything
+ // work with a clone so we don't screw up the iterator.
+ $clone = clone $this;
+
+ // get a copy of each data element
$data = array();
- foreach ($this->_data as $key => $val) {
+ foreach ($clone as $key => $val) {
if ($val instanceof Solar_Sql_Model_Record) {
$data[$key] = $val->toArray();
} else {
@@ -668,4 +683,28 @@
return $valid;
}
+
+ public function form($cols = null)
+ {
+ $array_name = $this->_model->model_name;
+ if (empty($cols)) {
+ $cols = '*';
+ }
+
+ $form = Solar::factory('Solar_Form');
+ $form->load('Solar_Form_Load_Model', $this->_model, $cols, $array_name);
+ $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') {
+ $form->setStatus(false);
+ }
+
+ if ($this->_status == 'inserted' || $this->_status == 'updated') {
+ $form->setStatus(true);
+ }
+
+ return $form;
+ }
}
\ No newline at end of file
Modified: branches/orm/Solar/Sql/Model.php
===================================================================
--- branches/orm/Solar/Sql/Model.php 2007-09-08 16:37:42 UTC (rev 2745)
+++ branches/orm/Solar/Sql/Model.php 2007-09-08 16:45:19 UTC (rev 2746)
@@ -705,6 +705,9 @@
* `page`
* : (int) Return only records from this page-number.
*
+ * `bind`
+ * : (array) Key-value pairs to bind into the query.
+ *
* @param array $params An array of parameters for the fetch, with keys
* for 'cols', 'where', 'group', 'having, 'order', and 'page'.
*
@@ -725,7 +728,8 @@
->having($params['having'])
->order($params['order'])
->setPaging($params['paging'])
- ->limitPage($params['page']);
+ ->limitPage($params['page'])
+ ->bind($params['bind']);
// fetch
$data = $select->fetchAll();
@@ -773,6 +777,8 @@
* `page`
* : (int) Return only records from this page-number.
*
+ * `bind`
+ * : (array) Key-value pairs to bind into the query.
*
* @param array $params An array of parameters for the fetch, with keys
* for 'cols', 'where', 'group', 'having, 'order', and 'page'.
@@ -794,7 +800,8 @@
->having($params['having'])
->order($params['order'])
->setPaging($params['paging'])
- ->limitPage($params['page']);
+ ->limitPage($params['page'])
+ ->bind($params['bind']);
// fetch
$data = $select->fetchAssoc();
@@ -830,6 +837,9 @@
* `order`
* : (string|array) ORDER BY these columns.
*
+ * `bind`
+ * : (array) Key-value pairs to bind into the query.
+ *
* @param array $params An array of parameters for the fetch, with keys
* for 'cols', 'where', 'group', 'having, and 'order'.
*
@@ -848,7 +858,8 @@
->multiWhere($params['where'])
->group($params['group'])
->having($params['having'])
- ->order($params['order']);
+ ->order($params['order'])
+ ->bind($params['bind']);
// fetch
$data = $select->fetchOne();
@@ -902,6 +913,9 @@
* `page`
* : (int) Return only records from this page-number.
*
+ * `bind`
+ * : (array) Key-value pairs to bind into the query.
+ *
* @param array $params An array of parameters for the fetch, with keys
* for 'cols', 'where', 'group', 'having, and 'order'.
*
@@ -922,7 +936,8 @@
->having($params['having'])
->order($params['order'])
->setPaging($params['paging'])
- ->limitPage($params['page']);
+ ->limitPage($params['page'])
+ ->bind($params['bind']);
// fetch
$data = $select->fetchCol();
@@ -963,6 +978,9 @@
* `page`
* : (int) Return only elements from this page-number.
*
+ * `bind`
+ * : (array) Key-value pairs to bind into the query.
+ *
* @param array $params An array of parameters for the fetch, with keys
* for 'cols', 'where', 'group', 'having, and 'order'.
*
@@ -983,7 +1001,8 @@
->having($params['having'])
->order($params['order'])
->setPaging($params['paging'])
- ->limitPage($params['page']);
+ ->limitPage($params['page'])
+ ->bind($params['bind']);
// fetch
$data = $select->fetchPairs();
@@ -996,6 +1015,67 @@
/**
*
+ * Fetches a single value from the model (i.e., the first column of the
+ * first record of the returned page set).
+ *
+ * Recognized parameters for the fetch are:
+ *
+ * `cols`
+ * : (string|array) Return only these columns; only the first one will
+ * be honored.
+ *
+ * `where`
+ * : (string|array) A Solar_Sql_Select::multiWhere() value parameter to
+ * restrict which records are returned.
+ *
+ * `group`
+ * : (string|array) GROUP BY these columns.
+ *
+ * `having`
+ * : (string|array) HAVING these column values.
+ *
+ * `order`
+ * : (string|array) ORDER BY these columns.
+ *
+ * `paging`
+ * : (int) Return this many records per page.
+ *
+ * `page`
+ * : (int) Return only elements from this page-number.
+ *
+ * `bind`
+ * : (array) Key-value pairs to bind into the query.
+ *
+ * @param array $params An array of parameters for the fetch, with keys
+ * for 'cols', 'where', 'group', 'having, and 'order'.
+ *
+ * @return mixed The single value from the model query, or null.
+ *
+ */
+ public function fetchValue($params = array())
+ {
+ // setup
+ $params = $this->_fixSelectParams($params);
+ $select = $this->newSelect($params['eager']);
+ $col = current($params['cols']);
+
+ // build
+ $select->distinct($params['distinct'])
+ ->from("{$this->_table_name} AS {$this->_model_name}", $col)
+ ->multiWhere($params['where'])
+ ->group($params['group'])
+ ->having($params['having'])
+ ->order($params['order'])
+ ->setPaging($params['paging'])
+ ->limitPage($params['page'])
+ ->bind($params['bind']);
+
+ // fetch
+ return $select->fetchValue();
+ }
+
+ /**
+ *
* Returns a new record with default values.
*
* @param array $spec An array of user-specified data to place into the
@@ -1061,7 +1141,8 @@
->multiWhere($params['where'])
->group($params['group'])
->having($params['having'])
- ->setPaging($this->_paging);
+ ->setPaging($this->_paging)
+ ->bind($params['bind']);
$col = "{$this->_model_name}.{$this->_primary_col}";
@@ -1089,7 +1170,8 @@
$select->multiWhere($params['where'])
->group($params['group'])
->having($params['having'])
- ->setPaging($params['paging']);
+ ->setPaging($params['paging'])
+ ->bind($params['bind']);
$opts = $this->related[$name];
$col = "{$opts['foreign_alias']}.{$opts['foreign_primary_col']}";
@@ -1154,6 +1236,10 @@
$params['page'] = null;
}
+ if (empty($params['bind'])) {
+ $params['bind'] = null;
+ }
+
return $params;
}
@@ -1403,7 +1489,7 @@
* object.
*
*/
- public function fetchRelatedObject($spec, $name, $page = null)
+ public function fetchRelatedObject($spec, $name, $page = null, $bind = null)
{
// fetch the related data as an array
$data = $this->fetchRelatedArray($spec, $name, $page);
@@ -1432,9 +1518,10 @@
return $result;
}
- public function fetchRelatedArray($spec, $name, $page = null)
+ public function fetchRelatedArray($spec, $name, $page = null, $bind = null)
{
$select = $this->newSelectRelated($spec, $name);
+ $select->bind($bind);
// fetch per the association type
$opts = $this->_related[$name];
@@ -1619,7 +1706,7 @@
$this->_sql->insert($this->_table_name, $data);
// no exception, it worked!
- $record->setStatus('clean');
+ $record->setStatus('inserted');
// if there was an autoincrement column, set its value in the data.
foreach ($this->_table_cols as $key => $val) {
@@ -1646,7 +1733,7 @@
/**
* Done! If clean, the save actually worked, otherwise it failed.
*/
- return $record->getStatus() == 'clean';
+ return $record->getStatus() == 'inserted';
}
/**
@@ -1716,7 +1803,7 @@
// attempt the update
try {
$this->_sql->update($this->_table_name, $data, $where);
- $record->setStatus('clean');
+ $record->setStatus('updated');
} catch (Solar_Sql_Adapter_Exception_QueryFailed $e) {
// something went wrong at the database
$record->setStatus('invalid');
@@ -1732,7 +1819,7 @@
/**
* Done!
*/
- return $record->getStatus() == 'clean';
+ return $record->getStatus() == 'updated';
}
/**
More information about the Solar-svn
mailing list