izniburak / pdox

useful query builder class that works with PHP PDO class for simple access your data.
http://burakdemirtas.org/pdox-kullanisli-pdo-sinifi-php/
MIT License
299 stars 105 forks source link

question UPDATE user SET user_points = user_points + 1 ... #104

Closed elcarim closed 2 years ago

elcarim commented 2 years ago

question! UPDATE user SET user_points = user_points + 1 WHERE user_id = 123 thx

elcarim commented 2 years ago

UPDATE user SET user_points = user_points + 10 WHERE user_id = 123

$db-> table( 'user' )->where( 'user_id', 123 ) -> update ( ['user_points' => ['+ 10']] )

public function update(array $data, $type = false)
{
    $query = 'UPDATE ' . $this->from . ' SET ';
    $values = [];

    foreach ($data as $column => $val) {
        if (is_array($val)) 
    $values[] = $column . '=' . $column . ' '. $val[0];
        else
            $values[] = $column . '=' . $this->escape($val);
    }
    $query .= implode(',', $values);

    if (!is_null($this->where)) {
        $query .= ' WHERE ' . $this->where;
    }

    if (!is_null($this->orderBy)) {
        $query .= ' ORDER BY ' . $this->orderBy;
    }

    if (!is_null($this->limit)) {
        $query .= ' LIMIT ' . $this->limit;
    }

    return $type === true ? $query : $this->query($query, false);
}
izniburak commented 2 years ago

Hi @elcarim Nice solution. But I've been working on a new version which has new capabilities that solve problems like that. It will release it in next weeks but I can't say exact date right now. Until to new version, you can create a new class that extend Pdox and override the update method to use your solution.

Thanks.

elcarim commented 2 years ago

thanks for pdox. it is amazing! putin-huylo!