Beispiel #1
0
 protected function _saveObjectHelper($insert, RM_Store_Object $object, RM_Validator_iValidator $validator = NULL)
 {
     if (isNull($validator)) {
         $validator = $object->validator();
     }
     if (!isNull($validator) && !$validator->check($object->props())) {
         return FALSE;
     }
     $meta = $this->_mediator->meta;
     $fields = array();
     $allFields = array();
     $props = $object->props();
     foreach ($meta->propList() as $prop) {
         $allFields[$meta->dbField($prop)] = $props[$prop];
         if ($insert or $props[$prop] !== $object->_propInitial($prop)) {
             $field = $meta->dbQuoted($prop);
             $fields[$insert ? $field : "{$field} = ?"] = $props[$prop];
         }
     }
     if (!$fields) {
         return FALSE;
     }
     if ($insert) {
         $binds = $fields;
         $query = "INSERT INTO {$this->_table}(" . join(',', array_keys($fields)) . ") VALUES(" . sqlBinds($fields) . ")";
     } else {
         list($where, $wbinds) = $this->_getDbWhere($object);
         $binds = array_merge($fields, $wbinds);
         $query = "UPDATE {$this->_table} SET " . join(',', array_keys($fields)) . " WHERE {$where}";
     }
     $this->_dbh->exec($query, array_values($binds));
     if (!isNull($this->_tableHistory)) {
         $obHistory = M('Store')->history();
         if ($obHistory->_closeStamp() || $object->isChildren()) {
             list($where, $wbinds) = $this->_getDbWhere($object);
             $query = "UPDATE {$this->_tableHistory} SET stamp_close = " . $obHistory->getStamp() . ", last_state = 0 WHERE " . $where . " AND last_state = 1";
             $this->_dbh->exec($query, $wbinds);
         }
         $binds = array_merge(array('stamp_open' => $obHistory->getStamp(), 'stamp_close' => $obHistory->getFarFuture(), 'last_state' => 1), $allFields);
         $query = "REPLACE INTO {$this->_tableHistory}( " . join(',', array_keys($binds)) . ") VALUES(" . sqlBinds($binds) . ")";
         $this->_dbh->exec($query, array_values($binds));
         $obHistory->register($object, $insert ? RM_Store_History::OBJECT_CREATE : RM_Store_History::OBJECT_EDIT);
     }
     if ($this->_autoId and !$object->{$this->_autoId}) {
         $object->{$this->_autoId} = $this->_dbh->lastInsertID();
         $allFields[$meta->dbField($this->_autoId)] = $object->{$this->_autoId};
     }
     if ($this->_cacheNs) {
         $this->cacheUpdate($object, $allFields);
     }
     return TRUE;
 }