Skip to content

Commit

Permalink
添加使用说明 && 修改返回值
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Jan 30, 2018
1 parent ebfbb09 commit 895706e
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 14 deletions.
121 changes: 118 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ $ php yii migrate --migrationPath=@yiier/actionStore/migrations/
Usage
-----

OConfigure Controller class as follows : :
**Config**

Configure Controller class as follows : :

```php
use yiier\actionStore\actions\ActionAction;
Expand All @@ -52,8 +54,121 @@ class TopicController extends Controller
}
```

Url
**Url**

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

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


**ActiveDataProvider Demo 1**

Controller

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

class TopicController extends Controller
{
public function actionFavorite()
{
$dataProvider = new ActiveDataProvider([
'query' => ActionStore::find()
->where(['user_id' => 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**

create ActionStoreSearch.php extends ActionStore

```php
<?php
namespace common\models;


use yiier\actionStore\models\ActionStore;

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

Controller

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

class TopicController extends Controller
{
public function actionFavorite()
{
$dataProvider = new ActiveDataProvider([
'query' => ActionStoreSearch::find()
->joinWith('company')
->where(['user_id' => 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'],
]) ?>
```
8 changes: 3 additions & 5 deletions src/actions/ActionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ public function run()
$model->load(array_merge(Yii::$app->request->getQueryParams(), ['user_id' => Yii::$app->user->id]), '');
$model->validate();
if (!$model->errors) {
if ($counter = ActionStore::createAction($model)) {
Yii::$app->response->format = Response::FORMAT_JSON;
return $counter;
}
Yii::$app->response->format = Response::FORMAT_JSON;
return ['code' => 200, 'data' => ActionStore::createUpdateAction($model), 'message' => 'success'];
}
throw new Exception(json_encode($model->errors));
return ['code' => 500, 'data' => '', 'message' => json_encode($model->errors)];
}
}
}
18 changes: 12 additions & 6 deletions src/models/ActionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function attributeLabels()
* @return int
* @throws Exception
*/
public static function createAction($model)
public static function createUpdateAction($model)
{
$conditions = array_filter($model->attributes);
switch ($model->type) {
Expand All @@ -107,14 +107,18 @@ public static function createAction($model)
$data = array_merge(['type' => $model->type], $conditions);
break;
}
if ($model->type == self::CLAP_TYPE) {
$value = self::find()->filterWhere($data)->select('value')->scalar();
$model->value = $value + 1;
if ($value = self::find()->filterWhere($data)->select('value')->scalar()) {
if ($model->type == self::CLAP_TYPE) {
$model->value = $value + 1;
} else {
self::find()->filterWhere($data)->one()->delete();
return 0;
}
}
self::deleteAll($data);
$model->load($data, '');
if ($model->save()) {
return $model->resetCounter();
return (int)$model->resetCounter();
}
throw new Exception(json_encode($model->errors));
}
Expand All @@ -123,8 +127,10 @@ public static function createAction($model)
* @param $model ActionStore
* @return false|int
*/
public static function destroyAction($model)
public function destroyAction($model)
{
$data = $this->attributes;
unset($data['id'], $data['created_at'], $data['updated_at'], $data['value']);
return $model->delete();
}

Expand Down

0 comments on commit 895706e

Please sign in to comment.