Skip to content

Commit

Permalink
添加 actionClass 可选参数
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Feb 2, 2018
1 parent 85e8d21 commit 0e6fff7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 20 deletions.
71 changes: 67 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Usage
Configure Controller class as follows : :

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

class TopicController extends Controller
Expand All @@ -57,7 +58,7 @@ class TopicController extends Controller
]
];
}
}
}
```

**Url**
Expand Down Expand Up @@ -113,7 +114,7 @@ class TopicController extends Controller
'company' => $company,
]);
}
}
}
```

View
Expand Down Expand Up @@ -152,7 +153,7 @@ class ActionStoreSearch extends ActionStore
{
public function getCompany()
{
return $this->hasOne(FundCompany::className(), ['id' => 'model_id']);
return $this->hasOne(Company::className(), ['id' => 'model_id']);
}
}
```
Expand Down Expand Up @@ -181,7 +182,7 @@ class TopicController extends Controller
'dataProvider' => $dataProvider,
]);
}
}
}
```
View

Expand All @@ -194,3 +195,65 @@ View
'options' => ['class' => 'collection-wrap'],
]) ?>
```


**actionClass Demo**

Controller

```php
<?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
<?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]);
}
}
}
```
9 changes: 7 additions & 2 deletions src/actions/ActionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
namespace yiier\actionStore\actions;

use Yii;
use yii\db\Exception;
use yii\helpers\Json;
use yii\web\Response;
use yiier\actionStore\models\ActionStore;

class ActionAction extends \yii\base\Action
{
/**
* @var string
*/
public $actionClass = '\yiier\actionStore\models\ActionStore';

public function init()
{
parent::init();
Expand All @@ -27,7 +31,8 @@ public function run()
Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->loginUrl)->send();
} else {
Yii::$app->response->format = Response::FORMAT_JSON;
$model = new ActionStore();
/** @var ActionStore $model */
$model = Yii::createObject($this->actionClass);
$model->load(array_merge(Yii::$app->request->getQueryParams(), ['user_id' => Yii::$app->user->id]), '');
if ($model->validate()) {
return ['code' => 200, 'data' => ActionStore::createUpdateAction($model), 'message' => 'success'];
Expand Down
37 changes: 23 additions & 14 deletions src/models/ActionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class ActionStore extends \yii\db\ActiveRecord
*/
const CLAP_TYPE = 'clap';

/**
* @var string
*/
const VIEW_TYPE = 'view';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -95,42 +100,46 @@ public static function createUpdateAction($model)
$conditions = array_filter($model->attributes);
switch ($model->type) {
case self::LIKE_TYPE:
self::deleteAll(array_merge(['type' => self::DISLIKE_TYPE], $conditions));
self::findOne(array_merge(['type' => self::DISLIKE_TYPE], $conditions))->delete();
$data = array_merge(['type' => self::LIKE_TYPE], $conditions);
break;
case self::DISLIKE_TYPE:
self::deleteAll(array_merge(['type' => self::LIKE_TYPE], $conditions));
self::findOne(array_merge(['type' => self::LIKE_TYPE], $conditions))->delete();
$data = array_merge(['type' => self::DISLIKE_TYPE], $conditions);
break;

default:
$data = array_merge(['type' => $model->type], $conditions);
break;
}
if ($value = self::find()->filterWhere($data)->select('value')->scalar()) {
if ($model->type == self::CLAP_TYPE) {
$model->value = $value + 1;
if ($didModel = $model::find()->filterWhere($data)->one()) {
if (!in_array($didModel->type, [self::CLAP_TYPE, self::VIEW_TYPE])) {
return $didModel->delete();
} else {
self::find()->filterWhere($data)->one()->delete();
return 0;
$model = $didModel;
$data['value'] = $didModel->value + 1;
}
}
self::deleteAll($data);
$model->load($data, '');
$model->setAttributes($data);
if ($model->save()) {
return (int)$model->resetCounter();
unset($conditions['id'], $conditions['created_at'], $conditions['updated_at'], $conditions['value']);
return self::resetCounter($model->type, $conditions);
}
throw new Exception(json_encode($model->errors));
}


/**
* 返回计数器
* @param $type
* @param $conditions
* @return int
*/
public function resetCounter()
public static function resetCounter($type, $conditions)
{
$data = $this->attributes;
unset($data['id'], $data['created_at'], $data['updated_at'], $data['value']);
return self::find()->filterWhere($data)->select('value')->scalar();
return (int)self::find()
->filterWhere(array_merge(['type' => $type], $conditions))
->select('value')
->sum('value');
}
}

0 comments on commit 0e6fff7

Please sign in to comment.