dev-master
9999999-devSimple user storage
MIT
The Requires
extension yii2
1.0.1
1.0.1.0Simple user storage
MIT
The Requires
extension yii2
1.0.0
1.0.0.0Simple user storage
MIT
The Requires
extension yii2
Simple user storage
Simple storage of user settings, options & etc., (*1)
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)
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);
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); // ... }
UserStorageModelInterface
interface and when loading(saving) does not specify attributes, all safe attributes will be saved.Simple user storage
MIT
extension yii2
Simple user storage
MIT
extension yii2
Simple user storage
MIT
extension yii2