2017 © Pedro Peláez
 

yii2-extension yii2-action-store

ActionStore for Yii2

image

yiier/yii2-action-store

ActionStore for Yii2

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 30 % Grown

The README.md

ActionStore for Yii2

ActionStore for Yii2, (*1)

Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

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

or add, (*5)

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

to the require section of your composer.json file., (*6)

Migrations

Run the following command, (*7)

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

Usage

Config, (*8)

Configure Controller class as follows : :, (*9)

 [
                '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**
 
```html
POST http://xxxxxxxxxxxxxx/topic/do?type=clap&model=topic&model_id=1
```

`model` recommend use `Model::tableName()`  


http response success(code==200) return json:

```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:

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

Demo
------

**ActiveDataProvider Demo 1**

Controller

```php
 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


```php
= 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, (*10)

create ActionStoreSearch.php extends ActionStore, (*11)

hasOne(Company::className(), ['id' => 'model_id']);
    }
}
```

Controller

```php
 ActionStoreSearch::find()
                ->joinWith('company')
                ->where(['user_id' => Yii::$app->user->id, 'type' => 'favorite']),
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);
      

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

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

actionClass Demo, (*12)

Controller, (*13)

<?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, (*14)

<?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, (*15)

get user model_id count, (*16)

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

get all model_id count, (*17)

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

Use createUpdateAction, (*18)

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

The Versions

18/07 2018

dev-master

9999999-dev

ActionStore for Yii2

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2 action-store

18/07 2018

v0.3

0.3.0.0

ActionStore for Yii2

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2 action-store

02/02 2018

v0.2

0.2.0.0

ActionStore for Yii2

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2 action-store

30/01 2018

v0.1

0.1.0.0

ActionStore for Yii2

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2 action-store