Ejemplo n.º 1
0
 /**
  * Rebuilds the caches for the given class.
  *
  * @param string $className
  */
 private function rebuild($className)
 {
     $instance = new $className();
     if (method_exists($instance, 'countCaches')) {
         $this->info("Rebuilding [{$className}] count caches");
         $countCache = new CountCache($instance);
         $countCache->rebuild();
     }
     if (method_exists($instance, 'sumCaches')) {
         $this->info("Rebuilding [{$className}] sum caches");
         $sumCache = new SumCache($instance);
         $sumCache->rebuild();
     }
 }
Ejemplo n.º 2
0
 /**
  * Boot the trait and its event bindings when a model is created.
  */
 public static function bootSummable()
 {
     static::created(function ($model) {
         $sumCache = new SumCache($model);
         $sumCache->apply(function ($config) use($model, $sumCache) {
             $sumCache->updateCacheRecord($config, '+', $model->{$config['columnToSum']}, $model->{$config['foreignKey']});
         });
     });
     static::updated(function ($model) {
         (new SumCache($model))->update();
     });
     static::deleted(function ($model) {
         $sumCache = new SumCache($model);
         $sumCache->apply(function ($config) use($model, $sumCache) {
             $sumCache->updateCacheRecord($config, '-', $model->{$config['columnToSum']}, $model->{$config['foreignKey']});
         });
     });
 }