public function reset_priority() { $priority = $this->get('priority', FALSE); if (!$priority) { return; } Jelly::update($this->meta()->table())->set(array('priority' => DB::expr('priority - 1')))->where('priority', '>', $priority)->execute(); $this->priority = 0; }
/** * Implementation of Jelly_Field_Behavior_Saveable * * @param Jelly $model * @param mixed $value * @return void */ public function save($model, $value, $loaded) { // Empty relations to the default value Jelly::update($this->foreign['model'])->where($this->foreign['column'], '=', $model->id())->set(array($this->foreign['column'] => $this->default))->execute(); // Set the new relations if (!empty($value)) { // Update the ones in our list Jelly::update($this->foreign['model'])->where(':primary_key', '=', $value)->set(array($this->foreign['column'] => $model->id()))->execute(); } }
public function save($model, $value, $loaded) { $old = $model->get($this->name, FALSE); $mod = Arr::get($_POST, $this->name . '-mod', 0) ? 1 : 0; if ($loaded and $old == $value or $old == $value + $mod or !$mod and $old == $value - 1) { return $value; } if ($loaded) { $model->reset_priority(); if ($value > $old) { $value--; } } $value = $value + $mod; $value = $value > 0 ? $value : 1; Jelly::update($model->meta()->table())->set(array($this->name => DB::expr($this->name . ' + 1')))->where($this->name, '>=', $value)->execute(); return $value; }
/** * Creates or updates the current record. * * If $key is passed, the record will be assumed to exist * and an update will be executed, even if the model isn't loaded(). * * @param mixed $key * @return $this **/ public function save($key = NULL) { // Determine whether or not we're updating $data = ($this->_loaded or $key) ? $this->_changed : $this->_changed + $this->_original; if (!is_null($key)) { // There are no rules for this since it is a meta alias and not an actual field // but adding it allows us to check for uniqueness when lazy saving $data[':unique_key'] = $key; } // Set the key to our id if it isn't set if ($this->_loaded) { $key = $this->_original[$this->_meta->primary_key()]; } // Run validation $data = $this->validate($data); // These will be processed later $values = $relations = array(); // Iterate through all fields in original incase any unchanged fields // have save() behavior like timestamp updating... foreach ($this->_changed + $this->_original as $column => $value) { // Filters may have been applied to data, so we should use that value if (array_key_exists($column, $data)) { $value = $data[$column]; } $field = $this->_meta->fields($column); // Only save in_db values if ($field->in_db) { // See if field wants to alter the value on save() $value = $field->save($this, $value, (bool) $key); if ($value !== $this->_original[$column]) { // Value has changed (or has been changed by field:save()) $values[$field->name] = $value; } else { // Insert defaults if (!$key and !$this->changed($field->name) and !$field->primary) { $values[$field->name] = $field->default; } } } elseif ($this->changed($column) and $field instanceof Jelly_Field_Behavior_Saveable) { $relations[$column] = $value; } } // If we have a key, we're updating if ($key) { // Do we even have to update anything in the row? if ($values) { Jelly::update($this)->where(':unique_key', '=', $key)->set($values)->execute(); } } else { list($id) = Jelly::insert($this)->columns(array_keys($values))->values(array_values($values))->execute(); // Gotta make sure to set this $values[$this->_meta->primary_key()] = $id; } // Set the changed data back as original $this->_original = array_merge($this->_original, $this->_changed, $values); // We're good! $this->_loaded = $this->_saved = TRUE; $this->_retrieved = $this->_changed = array(); // Save the relations foreach ($relations as $column => $value) { $this->_meta->fields($column)->save($this, $value, (bool) $key); } return $this; }