Longman\TelegramBot\ConversationDB::update PHP Method

update() public static method

Update the conversation in the database
public static update ( string $table, array $fields_values, array $where_fields_values ) : boolean
$table string
$fields_values array
$where_fields_values array
return boolean
    public static function update($table, array $fields_values, array $where_fields_values)
    {
        if (!self::isDbConnected()) {
            return false;
        }
        //Auto update the field update_at
        $fields_values['updated_at'] = self::getTimestamp();
        //Values
        $update = '';
        $tokens = [];
        $tokens_counter = 0;
        $a = 0;
        foreach ($fields_values as $field => $value) {
            if ($a) {
                $update .= ', ';
            }
            ++$a;
            ++$tokens_counter;
            $update .= '`' . $field . '` = :' . $tokens_counter;
            $tokens[':' . $tokens_counter] = $value;
        }
        //Where
        $a = 0;
        $where = '';
        foreach ($where_fields_values as $field => $value) {
            if ($a) {
                $where .= ' AND ';
            } else {
                ++$a;
                $where .= 'WHERE ';
            }
            ++$tokens_counter;
            $where .= '`' . $field . '`= :' . $tokens_counter;
            $tokens[':' . $tokens_counter] = $value;
        }
        $query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
        try {
            $sth = self::$pdo->prepare($query);
            $status = $sth->execute($tokens);
        } catch (Exception $e) {
            throw new TelegramException($e->getMessage());
        }
        return $status;
    }