yiier / yii2-action-store

Yii2 的 ActionStore

安装: 73

依赖者: 0

建议者: 0

安全: 0

星标: 4

关注者: 4

分支: 1

开放问题: 0

类型:yii2-extension

v1.2.1 2020-04-19 05:51 UTC

This package is auto-updated.

Last update: 2024-09-19 16:20:52 UTC


README

Yii2 的 ActionStore

Latest Stable Version Total Downloads Latest Unstable Version License

安装

安装此扩展的首选方式是通过 Composer

运行以下命令

php composer.phar require --prefer-dist yiier/yii2-action-store "*"

或者在您的 composer.json 文件的 require 部分添加

"yiier/yii2-action-store": "*"

迁移

运行以下命令

$ php yii migrate --migrationPath=@yiier/actionStore/migrations/

使用

配置

按照以下方式配置控制器类:

<?php
use yiier\actionStore\actions\ActionAction;

class TopicController extends Controller
{
    public function actions()
    {
        return [
            'do' => [
                'class' => ActionAction::className(),
                'pairsType' => ['want','own'], // Optional,default ['like', 'dislike']
                'counterType' => ['apply'], // Optional,default ['view', 'clap']
                'successCallable' => function ($model){ }, // Optional
                'returnCallable' => function ($model){ return $model; }, // Optional
            ]
        ];
    }
}

URL

POST http://xxxxxxxxxxxxxx/topic/do?type=clap&model=topic&model_id=1

model 推荐使用 Model::tableName()

HTTP 响应成功(code==200)返回 JSON

{"code":200,"data":{"id":156,"type":"apply","value":29,"user_type":"user","user_id":7,"model":"xx","model_id":"256","created_at":1583827902,"updated_at":1583830173,"typeCounter":29},"message":"success"}

HTTP 响应失败(code==500)返回 JSON

{"code":500,"data":"","message":"{\"model_id\":[\"Model ID不能为空。\"]}"}

演示

ActiveDataProvider 演示 1

控制器

<?php
use yiier\actionStore\actions\ActionAction;

class TopicController extends Controller
{
    public function actionFavorite()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => ActionStore::find()
                ->where(['user_id' => Yii::$app->user->id, 'type' => 'favorite']),
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);
        
        $ids = ArrayHelper::getColumn($dataProvider->getModels(), 'model_id');
        $company = ArrayHelper::index(Company::findAll($ids), 'id');

        return $this->render('favorite', [
            'dataProvider' => $dataProvider,
            'company' => $company,
        ]);
    }
}

视图

<?= yii\widgets\ListView::widget([
    'dataProvider' => $dataProvider,
    'itemOptions' => ['class' => 'list-group-item'],
    'summary' => false,
    'itemView' => function ($model, $key, $index, $widget) use ($company) {
        return $this->render('_favorite', [
            'model' => $model,
            'key' => $key,
            'index' => $index,
            'company' => $company[$model->model_id],
        ]);
    },
    'options' => ['class' => 'list-group'],
]) ?>

ActiveDataProvider 演示 2

创建 ActionStoreSearch.php,扩展 ActionStore

<?php
namespace common\models;


use yiier\actionStore\models\ActionStore;

class ActionStoreSearch extends ActionStore
{
    public function getCompany()
    {
        return $this->hasOne(Company::className(), ['id' => 'model_id']);
    }
}

控制器

<?php
use common\models\ActionStoreSearch;

class TopicController extends Controller
{
    public function actionFavorite()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => ActionStoreSearch::find()
                ->joinWith('company')
                ->where(['user_id' => Yii::$app->user->id, 'type' => 'favorite']),
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);
      

        return $this->render('favorite', [
            'dataProvider' => $dataProvider,
        ]);
    }
}

视图

<?= ListView::widget([
    'dataProvider' => $dataProvider,
    'itemOptions' => ['class' => 'collec-items clearfix'],
    'summary' => false,
    'itemView' => '_favorite',
    'options' => ['class' => 'collection-wrap'],
]) ?>

操作类演示

控制器

<?php
use common\models\ActionStoreSearch;
use yiier\actionStore\actions\ActionAction;

class TopicController extends Controller
{
    public function actions()
    {
        return [
            'do' => [
                'class' => ActionAction::className(),
                'actionClass' => ActionStoreSearch::className()
            ]
        ];
    }
}

ActionStoreSearch.php

<?php
use yiier\actionStore\models\ActionStore;

class ActionStoreSearch extends ActionStore
{
    /**
     * @var string
     */
    const FAVORITE_TYPE = 'favorite';

    public function getCompany()
    {
        return $this->hasOne(Company::className(), ['id' => 'model_id']);
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        if ($insert) {
            if ($this->type == self::FAVORITE_TYPE && $this->model == Company::tableName()) {
                Company::updateAllCounters(['favorite_count' => 1], ['id' => $this->model_id]);
            }
        }
    }

    public function afterDelete()
    {
        parent::afterDelete();
        if ($this->type == self::FAVORITE_TYPE && $this->model == Company::tableName()) {
            Company::updateAllCounters(['favorite_count' => -1], ['id' => $this->model_id]);
        }
    }
}

获取计数

获取用户 model_id 的数量

ActionStore::getCounter(
    ActionStoreSearch::FAVORITE_TYPE,
    ['model' => Company::tableName(), 'model_id' => $company->id, 'user_id' => \Yii::$app->user->id]
);

获取所有 model_id 的数量

ActionStore::getCounter(
    ActionStoreSearch::FAVORITE_TYPE,
    ['model' => Company::tableName(), 'model_id' => $company->id]
);

使用 createUpdateAction

$actionStore = new ActionStore();
$actionStore->setAttributes([
    'type' => 'download',
    'model' => Resource::tableName(),
    'model_id' => $id
]);
$actionStore->createUpdateAction($actionStore);