Skip to content

Commit 46d5baa

Browse files
committed
Fixed HMSET with NULL values, added doc & tests.
1 parent 4647f0d commit 46d5baa

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,7 @@ $redis->hIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
16831683

16841684
## hMset
16851685
##### Description
1686-
Fills in a whole hash.
1686+
Fills in a whole hash. Non-string values are converted to string, using the standard `(string)` cast. NULL values are stored as empty strings.
16871687
##### Parameters
16881688
*key*
16891689
*members*: key → value array

redis.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,21 +3937,36 @@ PHP_METHOD(Redis, hMset)
39373937
element_count += 2;
39383938

39393939
/* key is set. */
3940-
if (Z_TYPE_PP(z_value_p) != IS_STRING) {
3941-
convert_to_string(*z_value_p);
3942-
}
3943-
if(*cmd) {
3944-
old_cmd = cmd;
3940+
zval *z_copy;
3941+
MAKE_STD_ZVAL(z_copy);
3942+
switch(Z_TYPE_PP(z_value_p)) {
3943+
3944+
case IS_OBJECT:
3945+
ZVAL_STRINGL(z_copy, "Object", 6, 1);
3946+
break;
3947+
3948+
case IS_ARRAY:
3949+
ZVAL_STRINGL(z_copy, "Array", 5, 1);
3950+
break;
3951+
3952+
default:
3953+
*z_copy = **z_value_p;
3954+
zval_copy_ctor(z_copy);
3955+
if(Z_TYPE_PP(z_value_p) != IS_STRING) {
3956+
convert_to_string(z_copy);
3957+
}
39453958
}
3959+
3960+
old_cmd = cmd;
39463961
cmd_len = redis_cmd_format(&cmd, "%s"
39473962
"$%d" _NL "%s" _NL
39483963
"$%d" _NL "%s" _NL
39493964
, cmd, cmd_len
39503965
, hkey_len-1, hkey, hkey_len-1
3951-
, Z_STRLEN_PP(z_value_p), Z_STRVAL_PP(z_value_p), Z_STRLEN_PP(z_value_p));
3952-
if(old_cmd) {
3953-
efree(old_cmd);
3954-
}
3966+
, Z_STRLEN_P(z_copy), Z_STRVAL_P(z_copy), Z_STRLEN_P(z_copy));
3967+
efree(old_cmd);
3968+
zval_dtor(z_copy);
3969+
efree(z_copy);
39553970
}
39563971

39573972
old_cmd = cmd;

tests/TestRedis.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,15 @@ public function testHashes() {
15791579
$this->assertTrue('abc' === $this->redis->hGet('h', 'z'));
15801580
$this->assertTrue(FALSE === $this->redis->hGet('h', 't'));
15811581

1582+
// check non-string types.
1583+
$this->redis->delete('h1');
1584+
$this->assertTrue(TRUE === $this->redis->hMSet('h1', array('x' => 0, 'y' => array(), 'z' => new stdclass(), 't' => NULL)));
1585+
$h1 = $this->redis->hGetAll('h1');
1586+
$this->assertTrue('0' === $h1['x']);
1587+
$this->assertTrue('Array' === $h1['y']);
1588+
$this->assertTrue('Object' === $h1['z']);
1589+
$this->assertTrue('' === $h1['t']);
1590+
15821591
}
15831592

15841593
public function testMultiExec() {

0 commit comments

Comments
 (0)