2016-12-12 1 views
1

Quand je veux utiliser yii\caching\DbCache dans mon code yii2, je reçois:Comment créer une table en utilisant ('cache') dans yii2 dbcache?

Error: Table "cache" does not exist.

Comment puis-je créer ce tableau?

config/web.php:

'cache' => [ 
     'class' => 'yii\caching\DbCache', 
    ], 

code du contrôleur:

$cache = Yii::$app->cache; 
    $duration = 30; 

    if($currency == 'USD') 
    { 
     // try retrieving $data from cache 
     $data = $cache->get('getCurrencyUSD'); 
     if($data === false) 
     { 
      $url = 'wsdl file address ...'; 
      $client = new SoapClient($url); 
      $data = $client->getCurrency('USD'); 
      $cache->set('getCurrencyUSD', $data, $duration); 
      return $data; 
     } 
    } 
+0

document pour le cache utilisation de DB http : //www.yiiframewo rk.com/doc-2.0/yii-caching-dbcache.html#$cacheTable-detail – dungphanxuan

Répondre

1

Créer une nouvelle migration et ajouter le following example

public function up() 
{ 
    $this->createTable('{{%cache}}', [ 
     'id' => $this->char(128)->notNull(), 
     'expire' => $this->integer()->null(), 
     'data' => 'BLOB', 
    ]); 

    $this->addPrimaryKey('pk-cache', '{{%cache}}', 'id'); 
} 

public function down() 
{ 
    $this->dropTable('{{%cache}}'); 
}