[Solar-svn] Revision 2713

pmjones at solarphp.com pmjones at solarphp.com
Thu Aug 16 14:13:59 CDT 2007


Branch: Solar_Sql_Model:

* [BRK] On insert and update, 'created' and 'updated' values are now forcibly
  populated, regardless of whether or not values already exist in the data..

* [ADD] On insert and update, 'inherit' value is added if inheritance is 
  turned on and the value does not already exist.

* [BRK] Insert and update now return true/false instead of throwing an exception
  when the record is not valid.

* [ADD] Insert and update now catch database-level errors, set the record as
  invalid, and add the returned database error message keyed on '*' in the
  record's array of invalid messages.




Modified: branches/orm/Solar/Sql/Model.php
===================================================================
--- branches/orm/Solar/Sql/Model.php	2007-08-16 19:09:32 UTC (rev 2712)
+++ branches/orm/Solar/Sql/Model.php	2007-08-16 19:13:59 UTC (rev 2713)
@@ -1318,10 +1318,8 @@
      * 
      * @param Solar_Sql_Model_Record $record The Record to insert.
      * 
-     * @return void
+     * @return bool
      * 
-     * @throws Solar_Sql_Model_Exception_Invalid
-     * 
      */
     public function insertRecord($record)
     {
@@ -1331,20 +1329,25 @@
         // needed for created/updated timestamps
         $now = date('Y-m-d H:i:s');
         
-        // auto-add a 'created' value if there is a 'created' column
-        // and its value is not already set.
+        // force the 'created' value if there is a 'created' column
         $key = $this->_created_col;
-        if ($key && empty($record->$key)) {
+        if ($key) {
             $record->$key = $now;
         }
         
-        // auto-add an 'updated' value if there is an 'updated' column
-        // and its value is not already set.
+        // force the 'updated' value if there is an 'updated' column
         $key = $this->_updated_col;
-        if ($key && empty($record->$key)) {
+        if ($key) {
             $record->$key = $now;
         }
         
+        // if inheritance is turned on, auto-set the inheritance value,
+        // if not already set.
+        $key = $this->_inherit_col;
+        if ($key && $this->_inherit_model && empty($record->$key)) {
+            $record->$key = $this->_inherit_model;
+        }
+        
         // auto-add sequence values
         foreach ($this->_sequence_cols as $key => $val) {
             if (empty($record->$key)) {
@@ -1355,11 +1358,13 @@
         }
         
         /**
-         * Validate and insert
+         * Validate, insert, and handle exceptions
          */
         
         // filter the data (sanitize and validate)
-        $record->validate();
+        if (! $record->validate()) {
+            return false;
+        }
         
         // convert to array for the SQL command
         $data = $record->toArray();
@@ -1375,30 +1380,41 @@
         // apply serializing to the data elements as needed, then attempt the
         // insert.
         $this->_serializeCols($data);
-        $this->_sql->insert($this->_table_name, $data);
         
-        /**
-         * Reload the record with the inserted data
-         */
-        // if there was an autoincrement column, set its value in the data.
-        foreach ($this->_table_cols as $key => $val) {
-            if ($val['autoinc']) {
-                // set the value and leave the loop (only one autoinc
-                // should be here anyway)
-                $data[$key] = $this->_sql->lastInsertId();
-                break;
+        // attempt to insert the record, and catch SQL exceptions.
+        try {
+            // insert, which may cause an exception
+            $this->_sql->insert($this->_table_name, $data);
+            
+            // no exception, it worked!
+            $record->setStatus('clean');
+            
+            // if there was an autoincrement column, set its value in the data.
+            foreach ($this->_table_cols as $key => $val) {
+                if ($val['autoinc']) {
+                    // set the value and leave the loop (only one autoinc
+                    // should be here anyway)
+                    $data[$key] = $this->_sql->lastInsertId();
+                    break;
+                }
             }
+            
+        } catch (Solar_Sql_Adapter_Exception_QueryFailed $e) {
+            // something went wrong at the database
+            $record->setStatus('invalid');
+            $record->setInvalid('*', $e->getInfo('pdo_text'));
         }
         
         // load back to the record; this will automatically unserialize cols.
         // @todo This does not reflect values from sql-based functions;
         // would need to re-select from the DB to get those.
+        // note that we reload even if there was an SQL exception
         $record->load($data);
         
         /**
-         * Done!
+         * Done!  If clean, the save actually worked, otherwise it failed.
          */
-        $record->setStatus('clean');
+        return $record->getStatus() == 'clean';
     }
     
     /**
@@ -1407,7 +1423,7 @@
      * 
      * @param Solar_Sql_Model_Record $record
      * 
-     * @return void
+     * @return bool
      * 
      */
     public function updateRecord($record)
@@ -1416,13 +1432,19 @@
          * Auto-set timestamps and sequences
          */
          
-        // auto-add an 'updated' value if there is an 'updated' column
-        // and its value is not already set.
+        // force the 'updated' value
         $key = $this->_updated_col;
-        if ($key && empty($record->$key)) {
+        if ($key) {
             $record->$key = date('Y-m-d H:i:s');
         }
         
+        // if inheritance is turned on, auto-set the inheritance value,
+        // if not already set.
+        $key = $this->_inherit_col;
+        if ($key && $this->_inherit_model && empty($record->$key)) {
+            $record->$key = $this->_inherit_model;
+        }
+        
         // auto-add sequence values
         foreach ($this->_sequence_cols as $key => $val) {
             if (empty($record->$key)) {
@@ -1433,11 +1455,13 @@
         }
         
         /**
-         * Validate and attempt the update
+         * Validate, update, and handle exceptions
          */
         
         // filter the data (sanitize and validate)
-        $record->validate($record);
+        if (! $record->validate($record)) {
+            return false;
+        }
         
         // convert to array for the SQL command
         $data = $record->toArray();
@@ -1452,14 +1476,21 @@
         // serialize columns that need it
         $this->_serializeCols($data);
         
-        // remove primary-key column from the data
+        // remove primary-key column from the data and build a WHERE clause
         $primary = $this->_primary_col;
         unset($data[$primary]);
-        
-        // attempt the update based on the primary-key value
         $where = array("$primary = ?" => $record->$primary);
-        $this->_sql->update($this->_table_name, $data, $where);
         
+        // attempt the update
+        try {
+            $this->_sql->update($this->_table_name, $data, $where);
+            $record->setStatus('clean');
+        } catch (Solar_Sql_Adapter_Exception_QueryFailed $e) {
+            // something went wrong at the database
+            $record->setStatus('invalid');
+            $record->setInvalid('*', $e->getInfo('pdo_text'));
+        }
+        
         // load back to the record; this will automatically unserialize cols.
         // 
         // @todo This does not reflect values from sql-based functions;
@@ -1469,7 +1500,7 @@
         /**
          * Done!
          */
-        $record->setStatus('clean');
+        return $record->getStatus() == 'clean';
     }
     
     /**




More information about the Solar-svn mailing list