Increment value

@param   string  $field  Field to increment
@param   double  $val    Increment value

@return  mixed   Returns $this when insert succeed and false on failure
481846
public function increment($field, $val) {
        if (!is_numeric($val)) return false;

        $sql_query = 'UPDATE ' . $this->_table . 
                     ' SET ' . $field . ' = ' . $field . ' + ' . (double)$val;
        
        // Where statements
        if (count($this->_where)) {
            $sql_query .= ' WHERE ';
            $sql_query .= implode($this->_where, ' AND ');
            
            // or statements
            if (count($this->_or)) {
                $sql_query .= ' OR ';
                $sql_query .= implode($this->_or, ' AND ');
            }
        }
        
        // Set the query
        $this->_sql_query = $sql_query;
        
        return $this;
    }
318907
public function insert(string $key, float $val): int
    {
        return $this->handle->zAdd($this->name, $val, $key);
    }
377749
public function prefilled($field, $value)
    {
        $this->fields[ $field ] = [
            'type'  => self::TYPE_PREFILLED_VALUE,
            'value' => $value
        ];

        return $this;
    }
624703
public function addField(Field $field)
    {
        $this->fields->set($field->getColumn(), $field);
        
        return $this;
    }
1112795
public function setFieldValue(string $field, int $value) : bool
        {
                return $this->_redis->zadd($this->_key, $value, $field) > 0 ? true : false;
        }
107575
public function min(string $field, $value)
    {
        $this->update();
        $this->mongoDbUpdate['$min'][$field] = $value;

        return $this;
    }
1197625
public function set($field, $value = null)
    {
        if (func_num_args() == 1) {
            if (is_array($field)) {
                foreach ($field as $key => $value) {
                    if ($key === '0' || $key === 0) {
                        $this->set($value);
                    } else {
                        $this->set($key, $value);
                    }
                }

                return $this;
            } elseif ($field instanceof self) {
                $this->data = $field->data;
                //$this->id = $field->id;

                return $this;
            } else {
                $value = $field;
                $field = $this->title_field;
            }
        }

        $field = $this->normalizeFieldName($field);

        $f = $this->hasField($field);

        try {
            if ($f && $this->hook('normalize', [$f, $value]) !== false) {
                $value = $f->normalize($value);
            }
        } catch (Exception $e) {
            $e->addMoreInfo('field', $field);
            $e->addMoreInfo('value', $value);
            $e->addMoreInfo('f', $f);

            throw $e;
        }

        $default_value = $f ? $f->default : null;

        $original_value = array_key_exists($field, $this->dirty) ? $this->dirty[$field] :
            ((isset($f) && isset($f->default)) ? $f->default : null);

        $current_value = array_key_exists($field, $this->data) ? $this->data[$field] : $original_value;

        if (gettype($value) == gettype($current_value) && $value == $current_value) {
            // do nothing, value unchanged
            return $this;
        }

        if ($f) {
            // perform bunch of standard validation here. This can be re-factored in the future.
            if ($f->read_only) {
                throw new Exception([
                    'Attempting to change read-only field',
                    'field' => $field,
                    'model' => $this,
                ]);
            }

            // enum property support
            if (isset($f->enum) && $f->enum && $f->type != 'boolean') {
                if ($value === '') {
                    $value = null;
                }
                if ($value !== null && !in_array($value, $f->enum, true)) {
                    throw new Exception([
                        'This is not one of the allowed values for the field',
                        'field' => $field,
                        'model' => $this,
                        'value' => $value,
                        'enum'  => $f->enum,
                    ]);
                }
            }

            // values property support
            if ($f->values) {
                if ($value === '') {
                    $value = null;
                } elseif ($value === null) {
                    // all is good
                } elseif (!is_string($value) && !is_int($value)) {
                    throw new Exception([
                        'Field can be only one of pre-defined value, so only "string" and "int" keys are supported',
                        'field' => $field,
                        'model' => $this,
                        'value' => $value,
                        'values'=> $f->values,
                    ]);
                } elseif (!array_key_exists($value, $f->values)) {
                    throw new Exception([
                        'This is not one of the allowed values for the field',
                        'field'   => $field,
                        'model'   => $this,
                        'value'   => $value,
                        'values'  => $f->values,
                    ]);
                }
            }
        }

        if (array_key_exists($field, $this->dirty) && (
            gettype($this->dirty[$field]) == gettype($value) && $this->dirty[$field] == $value
        )) {
            unset($this->dirty[$field]);
        } elseif (!array_key_exists($field, $this->dirty)) {
            $this->dirty[$field] =
                array_key_exists($field, $this->data) ?
                $this->data[$field] : $default_value;
        }
        $this->data[$field] = $value;

        return $this;
    }
1181340
public function decrement($key, $val = 1)
    {
        return $this->hash($key)->decrBy($key, abs(intval($val)));
    }
64544
public function addValue($field, $value) {
        $rfield = str_replace(['`', '"'], '', $field);
        $field  = Condition::safeField($field);
        $index  = isset ($this->names [ $field ]) ? $this->names [ $field ] : 0;
        $key    = ':' . $field . '_' . $index;
        if (is_string($value)) {//for bigint
            $type = \PDO::PARAM_STR;
        } else if (is_numeric($value)) {// for int float
            $type = \PDO::PARAM_INT;
        } else if (is_bool($value)) {
            $type  = \PDO::PARAM_INT;
            $value = intval($value);
        } else if (is_null($value)) {
            $type = \PDO::PARAM_NULL;
        } else {
            $type = \PDO::PARAM_STR;
        }
        $this->values []        = [$key, $value, $type, $field, $rfield];
        $this->names [ $field ] = ++$index;

        return $key;
    }
897413
public function increment($key, $val = 1, $field = null, $tablePrefix = null)
    {
        list($tableName, $condition) = $this->parseKey($key, true);
        if (is_null($field) || empty($tableName) || empty($condition)) {
            return false;
        }
        $val = abs(intval($val));
        is_null($tablePrefix) && $tablePrefix = $this->tablePrefix;
        $tableName = $tablePrefix . $tableName;

        $bulk = new BulkWrite();
        $bulk->update($condition, ['$inc' => [$field => $val]], ['multi' => true]);
        $result = $this->runMongoBulkWrite($tableName, $bulk);

        Cml::$debug && $this->debugLogSql('BulkWrite INC', $tableName, $condition, ['$inc' => [$field => $val]]);

        return $result->getModifiedCount();
    }
839285
public function exists($field, $val)
    {
        $db = $this->load();
        $result = false;
        foreach ($db as $index => $row) {
            if ($row[$field] === $val) {
                $result = true;
            }
        }
        return $result;
    }
299677
public function setValue($field, $value) {
        if (is_array($this->values)) {
            $this->values[$field] = $value;
        } elseif (is_object($this->values)) {
            $this->values->$field = $value;
        }
    }
669200
public function decrement($key, $val = 1)
    {
        return apc_dec($this->conf['prefix'] . $key, abs(intval($val)));
    }
1185159
public function set($key, $val = null)
    {
        if (is_array($key)) {
            $this->_setpost = array_merge($this->_setpost, $key);
        } else {
            $this->_setpost[$key] = $val;
        }
        return $this;
    }
576535
public function setField($fieldName, $val)
    {
        $this->objCacheClear();
        //if it's a has_one component, destroy the cache
        if (substr($fieldName, -2) == 'ID') {
            unset($this->components[substr($fieldName, 0, -2)]);
        }

        // If we've just lazy-loaded the column, then we need to populate the $original array
        if (isset($this->record[$fieldName . '_Lazy'])) {
            $tableClass = $this->record[$fieldName . '_Lazy'];
            $this->loadLazyFields($tableClass);
        }

        // Support component assignent via field setter
        $schema = static::getSchema();
        if ($schema->unaryComponent(static::class, $fieldName)) {
            unset($this->components[$fieldName]);
            // Assign component directly
            if (is_null($val) || $val instanceof DataObject) {
                return $this->setComponent($fieldName, $val);
            }
            // Assign by ID instead of object
            if (is_numeric($val)) {
                $fieldName .= 'ID';
            }
        }

        // Situation 1: Passing an DBField
        if ($val instanceof DBField) {
            $val->setName($fieldName);
            $val->saveInto($this);

            // Situation 1a: Composite fields should remain bound in case they are
            // later referenced to update the parent dataobject
            if ($val instanceof DBComposite) {
                $val->bindTo($this);
                $this->record[$fieldName] = $val;
            }
        // Situation 2: Passing a literal or non-DBField object
        } else {
            // If this is a proper database field, we shouldn't be getting non-DBField objects
            if (is_object($val) && $schema->fieldSpec(static::class, $fieldName)) {
                throw new InvalidArgumentException('DataObject::setField: passed an object that is not a DBField');
            }

            if (!empty($val) && !is_scalar($val)) {
                $dbField = $this->dbObject($fieldName);
                if ($dbField && $dbField->scalarValueOnly()) {
                    throw new InvalidArgumentException(
                        sprintf(
                            'DataObject::setField: %s only accepts scalars',
                            $fieldName
                        )
                    );
                }
            }

            // if a field is not existing or has strictly changed
            if (!array_key_exists($fieldName, $this->original) || $this->original[$fieldName] !== $val) {
                // TODO Add check for php-level defaults which are not set in the db
                // TODO Add check for hidden input-fields (readonly) which are not set in the db
                // At the very least, the type has changed
                $this->changed[$fieldName] = self::CHANGE_STRICT;

                if ((!array_key_exists($fieldName, $this->original) && $val)
                    || (array_key_exists($fieldName, $this->original) && $this->original[$fieldName] != $val)
                ) {
                    // Value has changed as well, not just the type
                    $this->changed[$fieldName] = self::CHANGE_VALUE;
                }
            // Value has been restored to its original, remove any record of the change
            } elseif (isset($this->changed[$fieldName])) {
                unset($this->changed[$fieldName]);
            }

            // Value is saved regardless, since the change detection relates to the last write
            $this->record[$fieldName] = $val;
        }
        return $this;
    }
158096
public function addToSet(string $field, $value)
    {
        $this->update();
        $this->mongoDbUpdate['$addToSet'][$field] = $value;

        return $this;
    }
332934
public function count($field = '*')
    {
        $copy = clone $this;

        $copy->_limit = null;
        $copy->_offset = null;
        $copy->_order = null;
        $copy->_aggregate['count'] = ['$sum' => 1];
        $r = $copy->execute();

        return $r ? $r[0]['count'] : 0;
    }