Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis hashes as php array #39

Open
theKerosin opened this issue Oct 13, 2015 · 7 comments
Open

redis hashes as php array #39

theKerosin opened this issue Oct 13, 2015 · 7 comments
Assignees
Labels
severity:BC breaking Breaks backwards compatibility type:enhancement Enhancement

Comments

@theKerosin
Copy link

I propose to explicitly define next methods in \yii\redis\Connection

public function hSet($key, array $data);
public function hMset($key, array $data);
public function hGetAll($key);

To work with redis hashes as php associative arrays
e.g.

#set
$redis->hMset('someKey', ['hello' => 'world', 'someKey' => 'someValue']);
#get
$redis->hGetAll('someKey');
#return ['hello' => 'world', 'someKey' => 'someValue'], instead of  ['hello', 'world', 'someKey', 'someValue']
@samdark
Copy link
Member

samdark commented Oct 13, 2015

Good idea. @cebe would it break BC?

@samdark samdark added the type:enhancement Enhancement label Oct 13, 2015
theKerosin added a commit to theKerosin/yii2-redis that referenced this issue Oct 13, 2015
@cebe cebe self-assigned this Oct 20, 2015
@cebe cebe added this to the 2.0.5 milestone Oct 20, 2015
@Insolita
Copy link

And not only hashes - zrange, zrevrange .etc with options WITHSCORES too

        $redis->executeCommand('ZREVRANGE',['myzset', 0, 2, 'WITHSCORES']);
        #return ['value1',5,'value2',4]   best expected ['value1'=>5, 'value2'=>4]

@theKerosin
Copy link
Author

Not completely clear but it may be implemented as separate method

@cebe cebe modified the milestones: 2.0.x, 2.0.5 Mar 1, 2016
@cebe
Copy link
Member

cebe commented Mar 1, 2016

this will break existing magic methods.

@cebe cebe added the severity:BC breaking Breaks backwards compatibility label Mar 1, 2016
@lyt8384
Copy link

lyt8384 commented Dec 1, 2016

public function hSet($key, array $data);
It is recommended to
public function hSet($key, $data, $value = '');
to compatible $redis->hSet('somekey','field','value');

@cebe cebe modified the milestones: 2.0.x, 2.1.0 Dec 5, 2017
@samdark samdark removed this from the 2.1.0 milestone Sep 23, 2018
@yanhuixie
Copy link

Hi, all~
You may simply add a helper function in your extended ArrayHelper etc.

    /**
     * post process for redis hgetall, rebind the key-value relation
     * @param array $arr
     * @param array $decodeFileds the filed names need json_decode
     * @return NULL|[]
     * @author xieyh
     */
    public static function postRedisHgetall($arr, $decodeFileds = null) {
        if(empty($arr)) return null;
        $cnt = count($arr);
        $data = [];
        for($i = 0; $i < $cnt; $i += 2){
            $filed = $arr[$i];
            $value = $arr[$i+1];
            if($decodeFileds && in_array($filed, $decodeFileds)){
                $value = json_decode($value, TRUE);
            }
            $data[$filed] = $value;
        }
        return $data;
    }

@curtis18
Copy link

The feature of returning associative array is useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity:BC breaking Breaks backwards compatibility type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

7 participants