예제 #1
0
 public function sync(RM_Store_Object $object)
 {
     $oid = "" . $object;
     $props = $object->props();
     $info = isset($this->_objCache[$oid]) ? $this->_objCache[$oid] : array();
     $info['.'] = $object;
     foreach ($this->_mediator->meta->keyList() as $key) {
         # remove old bindings
         if (isset($info[$key])) {
             unset($this->_keyCache[$key][$info[$key]]);
         }
         # calc hash
         $id = $this->_mediator->meta->keyIdByHash($key, $props);
         # update bindings
         $info[$key] = $id;
         if ($id) {
             if (isset($this->_keyCache[$key][$id])) {
                 throw new RM_Base_Exception_BadUsage("Integrity constraint violation. class=" . get_class($object) . ", key={$key}, id={$id}. Object #{$oid} conflicts with object #" . $this->_keyCache[$key][$id]);
             }
             $this->_keyCache[$key][$id] = $object;
         }
     }
     $this->_objCache[$oid] = $info;
     return $object;
 }
예제 #2
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;
 }
예제 #3
0
파일: shortcuts.php 프로젝트: evilgeny/bob
/**
 * Safe update of object by data array
 *
 * @param RM_Store_Object $object
 * @param array $data
 */
function updateObjectByArray(RM_Store_Object $object, array $data)
{
    foreach (array_keys($object->props()) as $property) {
        if ($property == 'id') {
            continue;
        }
        // чтоб случайно id не подменить
        if (isset($data[$property])) {
            $object->{$property} = $data[$property];
        }
    }
}