Yii2: set method(the second call) will throw exception when use DbCache component

Created on 21 Nov 2017  Â·  2Comments  Â·  Source: yiisoft/yii2

as you suggest,I create table cache:

      CREATE TABLE cache (
          id char(128) NOT NULL PRIMARY KEY,
          expire int(11),
          data BLOB
      );

because the id field is primary key,that is the problem:
Integrity constraint violation: 1062 Duplicate entry 'key' for key 'PRIMARY'

What steps will reproduce the problem?

//first time call
$cache->set('key','some data');
//the second call
$cache->set('key','some data');

What is the expected result?

null

What do you get instead?

I find DbCache php file, change that setValue() method,add a line below:

protected function setValue($key, $value, $duration)
    {
        $command = $this->db->createCommand()
            ->update($this->cacheTable, [
                'expire' => $duration > 0 ? $duration + time() : 0,
                'data' => [$value, \PDO::PARAM_LOB],
            ], ['id' => $key]);
        if ($command->execute()) {
            $this->gc();
            return true;
        } else {
        /*======add this line===========*/
        $this->deleteValue($key);//before addValue,delete the  duplicate key if any.
        /*========================*/**
            return $this->addValue($key, $value, $duration);
        }
    }

that is the revolution。

Additional info

Integrity constraint violation: 1062 Duplicate entry 'key' for key 'PRIMARY'
when I use FileCache,it won't throw exception,and It will refresh the data every call set method。
| Q | A
| ---------------- | ---
| Yii version | 2.0.11.2
| PHP version | 7.0
| Operating system | Windows

to be verified bug

Most helpful comment

@samdark I already fixed this problem in early 2018. See commit 13ad205, PR #16002. Upsert is now used instead of insert/update.

Related issue #14660.

It can be closed.

All 2 comments

MySQL will return 0 on an UPDATE execute() if no data is changed.
This behaviour can be configured as per this comment but won't be applicable to other db's http://php.net/manual/en/pdostatement.rowcount.php#104930

@samdark I already fixed this problem in early 2018. See commit 13ad205, PR #16002. Upsert is now used instead of insert/update.

Related issue #14660.

It can be closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

schmunk42 picture schmunk42  Â·  3Comments

chaintng picture chaintng  Â·  3Comments

jpodpro picture jpodpro  Â·  3Comments

kminooie picture kminooie  Â·  3Comments

MUTOgen picture MUTOgen  Â·  3Comments