Skip to content

Instantly share code, notes, and snippets.

@demisang
Created December 16, 2017 15:19
Show Gist options
  • Save demisang/10c775ce4872f115136c81d7f535f356 to your computer and use it in GitHub Desktop.
Save demisang/10c775ce4872f115136c81d7f535f356 to your computer and use it in GitHub Desktop.
<?php
return [
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'balanceManager' => [
'class' => 'yii2tech\balance\ManagerDb',
'accountTable' => '{{%BalanceAccount}}',
'transactionTable' => '{{%BalanceTransaction}}',
'accountLinkAttribute' => 'accountId',
'amountAttribute' => 'amount',
'dataAttribute' => 'data',
],
],
];
<?php
namespace frontend\controllers;
use Yii;
use yii\base\InvalidParamException;
use yii\helpers\VarDumper;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex()
{
/** @var \yii2tech\balance\ManagerDb $balanceManager */
$balanceManager = Yii::$app->balanceManager;
$user1 = 1;
$user2 = 2;
// add 500 credits to $user1
$balanceManager->increase($user1, 500);
// remove 100 credits from $user1
$balanceManager->decrease($user1, 100);
// remove 100 credits from $user1 and add 100 credits to $user2
$balanceManager->transfer($user1, $user2, 100);
VarDumper::dump([
'user1balance' => $balanceManager->calculateBalance($user1),
'user2balance' => $balanceManager->calculateBalance($user2),
], 10, true);
die;
}
/// ...
}
<?php
use yii\db\Migration;
/**
* Class m171216_150421_create_balance_tables
*/
class m171216_150421_create_balance_tables extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$this->createTable('BalanceAccount', [
'id' => $this->primaryKey(),
'balance' => $this->integer()->notNull()->defaultValue(0),
// ...
]);
$this->createTable('BalanceTransaction', [
'id' => $this->primaryKey(),
'date' => $this->integer()->notNull(),
'accountId' => $this->integer()->notNull(),
'extraAccountId' => $this->integer()->notNull()->defaultValue(0),
'amount' => $this->integer()->notNull()->defaultValue(0),
'data' => $this->text(),
// ...
]);
}
/**
* @inheritdoc
*/
public function safeDown()
{
$this->dropTable('BalanceTransaction');
$this->dropTable('BalanceAccount');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment