Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
<?php
/**
 * Created by PhpStorm.
 * User: danilo
 * Date: 02/03/16
 * Time: 14:07
 */

namespace Modules\Core\Database\Behaviours\CountCache;

use Modules\Core\Database\Behaviours\Cacheable;

trait Countable
{
    use Cacheable;

    /**
     * Boot the countable behaviour and setup the appropriate event bindings.
     */
    public static function bootCountable()
    {
        static::created(function ($model) {
            $countCache = new CountCache($model);
            $countCache->apply(function ($config) use ($countCache, $model) {
                $countCache->updateCacheRecord($config, '+', 1, $model->{$config['foreignKey']});
            });
        });
        static::updated(function ($model) {
            (new CountCache($model))->update();
        });
        static::deleted(function ($model) {
            $countCache = new CountCache($model);
            $countCache->apply(function ($config) use ($countCache, $model) {
                $countCache->updateCacheRecord($config, '-', 1, $model->{$config['foreignKey']});
            });
        });
    }

    /**
     * Return the count cache configuration for the model.
     *
     * @return array
     */
    abstract public function countCaches();
}