PHP code example of hunter-kaan / yii2-user-storage
1. Go to this page and download the library: Download hunter-kaan/yii2-user-storage library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// 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);
$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();
// ...
}
class Post extends ActiveRecord implements UserStorageModelInterface
{
// ...
/**
* Storage safe attributes.
*
* @return array
*/
public function userStorageAttributes()
{
return ['pinned'];
}
// ...
// 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']);
// ...
}
// 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);
// ...
}