2017 © Pedro Peláez
 

yii2-extension yii2-user-storage

Simple user storage

image

hunter-kaan/yii2-user-storage

Simple user storage

  • Thursday, February 15, 2018
  • by hunter-kaan
  • Repository
  • 1 Watchers
  • 0 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 25 % Grown

The README.md

Yii2 User storage

Simple storage of user settings, options & etc., (*1)

Installation

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

Either run, (*3)

php composer.phar require --prefer-dist hunter-kaan/yii2-user-storage "^1.0"

or add, (*4)

"hunter-kaan/yii2-user-storage": "^1.0"

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

Configuration

To use this extension, you have to configure the Storage class in your application configuration:, (*6)

return [
    //....
    'components' => [
        'userStorage' => [
            'class' => HunterKaan\UserStorage\Storage::class,
            'keyPrefix' => '',
            'userStorageTable' => '{{%user_storage}}',
            'userTable' => '{{%user}}',
        ],
    ]
    //....
];

Before you can go on you need to create those tables in the database. To do this, you can use the migration stored in @vendor/hunter-kaan/yii2-user-storage/migrations:, (*7)

php yii migrate/up --migrationPath=@vendor/hunter-kaan/yii2-user-storage/migrations

The component extends the yii\caching\Cache class., (*8)

A typical usage is like the following:, (*9)

    // To get default form values.
    $formDefaultValues = Yii::$app->userStorage->get('myForm-default-values');

    // To store user choose as default form values.
    $formDefaultValues = ['city_id' => 3];
    Yii::$app->userStorage->set('myForm-options', $formDefaultValues);

Examples

Model service

Store and load model data:, (*10)

In your controller:, (*11)

    $model = new Post();
    $model->loadDefaultValues();

    // Create model service
    $storageService = Yii::$app->userStorage->buildModelService($model);

    // Load data from user storage to model
    $storageService->load();

    // ...
    if ($model->load(Yii::$app->getRequest()->getBodyParam()) && $model->save()) {
        // Save user data to storage
        $storageService->save();
        // ...
    }

The model must implement interface HunterKaan\UserStorage\UserStorageModelInterface:, (*12)

class Post extends ActiveRecord implements UserStorageModelInterface
{
    // ...

    /**
     * Storage safe attributes.
     *
     * @return array
     */
    public function userStorageAttributes()
    {
        return ['pinned'];
    }
    // ...

or you must specify safe attributes when loading(saving):, (*13)

    // Create model service
    $storageService = Yii::$app->userStorage->buildModelService($model);

    // Load data from user storage to model
    $storageService->load(null, ['pinned']);
    // ...
    if ($model->load(Yii::$app->getRequest()->getBodyParam()) && $model->save()) {
        // Save user data to storage
        $storageService->save(null, ['pinned']);
        // ...
    }

By default model storage key is YourModel::class, but it can be redefined:, (*14)

    // Create model service
    $storageService = Yii::$app->userStorage->buildModelService($model);

    // Load data from user storage to model
    $storageService->load(CommonModel::class);
    // ...
    if ($model->load(Yii::$app->getRequest()->getBodyParam()) && $model->save()) {
        // Save user data to storage
        $storageService->save(CommonModel::class);
        // ...
    }

Note

  • Only secure attributes can be saved. See models guide.
  • If the model does not implement the UserStorageModelInterface interface and when loading(saving) does not specify attributes, all safe attributes will be saved.
  • At the moment, only storage in the database is supported.

The Versions

15/02 2018

dev-master

9999999-dev

Simple user storage

  Sources   Download

MIT

The Requires

 

extension yii2

15/02 2018

1.0.1

1.0.1.0

Simple user storage

  Sources   Download

MIT

The Requires

 

extension yii2

12/02 2018

1.0.0

1.0.0.0

Simple user storage

  Sources   Download

MIT

The Requires

 

extension yii2