定制 yiier/yii2-action-store 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yiier/yii2-action-store

Composer 安装命令:

composer require yiier/yii2-action-store

包简介

ActionStore for Yii2

README 文档

README

ActionStore for Yii2

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

Migrations

Run the following command

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

Usage

Config

Configure Controller class as follows : :

<?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 recommend use Model::tableName()

http response success(code==200) return 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 response failure(code==500) return json:

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

Demo

ActiveDataProvider Demo 1

Controller

<?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,
        ]);
    }
}

View

<?= 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 Demo 2

create ActionStoreSearch.php extends 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']);
    }
}

Controller

<?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,
        ]);
    }
}

View

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

actionClass Demo

Controller

<?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]);
        }
    }
}

Get Counter

get user model_id count

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

get all model_id count

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

Use createUpdateAction

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

统计信息

  • 总下载量: 73
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-4-Clause
  • 更新时间: 2018-01-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固