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

integer-string-integer pks bug #18

Open
vaseninm opened this issue May 26, 2015 · 1 comment · May be fixed by #19
Open

integer-string-integer pks bug #18

vaseninm opened this issue May 26, 2015 · 1 comment · May be fixed by #19
Assignees
Labels

Comments

@vaseninm
Copy link

Hi, i found a bug. Example:

class Bet extends \yii\redis\ActiveRecord
{
    public static function primaryKey()
    {
        return [
                   'game_id', //int
                   'stage_id', //string
                   'user_id' //int
       ];
    }
    // some code
}

        $bet1 = new Bet();
        $bet1->game_id = 1;
        $bet1->user_id = 1;
        $bet1->stage_id = 'flop';
        $bet1->sum = 0;
        $bet1->save();

        $bet2 = Bet::find()->where([
            'game_id' => 1,
            'user_id' => 1,
            'stage_id' => 'flop',
        ])->one();

        $bet2->sum = $bet2->sum + 10;

        $bet2->save(); // not saved, but return true
vaseninm added a commit to vaseninm/yii2-redis that referenced this issue May 26, 2015
@vaseninm vaseninm linked a pull request May 26, 2015 that will close this issue
@cebe cebe added this to the 2.0.5 milestone May 26, 2015
@cebe cebe self-assigned this May 26, 2015
vaseninm added a commit to vaseninm/yii2-redis that referenced this issue May 27, 2015
@cebe cebe modified the milestones: 2.0.x, 2.0.5 Mar 1, 2016
@nailfor
Copy link

nailfor commented Aug 19, 2016

solution is

    public static function buildKey($key)
    {
        if (is_numeric($key)) {
            return $key;
        } elseif (is_string($key)) {
            return ctype_alnum($key) && StringHelper::byteLength($key) <= 32 ? $key : md5($key);
        } elseif (is_array($key)) {
            if (count($key) == 1) {
                return self::buildKey(reset($key));
            }
            ksort($key); // ensure order is always the same
            $isNumeric = true;
            foreach ($key as $k => $value) {
                if (!is_numeric($value)) {
                    $isNumeric = false;
                }
                $key[$k] = strval($value); // <-THIS
            }
            if ($isNumeric) {
                return implode('-', $key);
            }
        }

        return md5(json_encode($key));
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants