错误在null上调用成员函数get()

时间:2018-05-27 06:44:42

标签: php yii2

大家晚上好!请帮助,在使用yii2创建的网站上我想创建一个用户的个人资金帐户..但是在授权用户时,它起飞了:

Call to a member function get() on null

链接到网站:coinfactory.pw

在网站上我有一个来自dektrium yii2-user

的模块

我重新定义了两个User.php和RegistrationForm.php模型:

user.php的

<?php

namespace app\models;

use dektrium\user\models\User as BaseUser;

class User extends BaseUser
{
public function getBalance()
{
    return $this->hasOne(UserBalance::className(), ['user_id' => 'id']);
}

RegistrationForm.php

<?php
namespace app\models;

use dektrium\user\models\RegistrationForm as BaseRegistrationForm;

class RegistrationForm extends BaseRegistrationForm
{
    public function signup()
    {
        \Yii::$app->db->transaction(function() {
            (new UserBalance([
                'user_id' => $this->id,
                'silver_in' => 100
            ]))->save();
            // increase setting
            $value = SettingHelper::get('user.count') + rand(1, 10);
            SettingHelper::set('user.count', $value);
        });
    }
}

重新定义web.php模型的配置站点:

'modules' => [
'rbac' => 'dektrium\rbac\RbacWebModule',
'user' => [
    'class' => \dektrium\user\Module::className(),
    // 'identityClass' => 'dektrium\user\models\User',
    // 'identityClass' => 'dektrium\user\models\RegistrationForm',
    'mailer' => [
        //'sender'                => 'no-reply@myhost.com', // or ['no-reply@myhost.com' => 'Sender name']
        'sender'                => 'example@yandex.ru',
        'welcomeSubject'        => 'Welcome subject',
        'confirmationSubject'   => 'Confirmation subject',
        'reconfirmationSubject' => 'Email change subject',
        'recoverySubject'       => 'Recovery subject',
    ],
    'controllerMap' => [
        'registration' => [
            'class' => \dektrium\user\controllers\RegistrationController::className(),
            'on ' . \dektrium\user\controllers\RegistrationController::EVENT_AFTER_REGISTER => function ($e) {
                Yii::$app->response->redirect(array('/user/security/login'))->send();
                Yii::$app->end();
            }
        ],
    ],
    'modelMap' => [
        'User' => 'app\models\User',
        'RegistrationForm' => 'app\models\RegistrationForm',
    ],
    'enableUnconfirmedLogin' => true,
    'enableFlashMessages' => true,
    'enableRegistration' => true,
    'enableGeneratingPassword' => true,
    'enableUnconfirmedLogin' => false,
    'enablePasswordRecovery' => true,
    'enableAccountDelete' => false,
    'emailChangeStrategy' => '\dektrium\user\Module::STRATEGY_DEFAULT',
    'recoverWithin' => 21600,
    'confirmWithin' => 21600,
    'cost' => 12,
    'admins' => ['admin']
],
],

UserBalance.php

<?php
namespace app\models;

use app\helpers\BalanceHelper;
use yii\base\InvalidParamException;
use yii\db\ActiveRecord;

class UserBalance extends ActiveRecord
{
    const CURRENCY_SILVER_IN = 'silver_in';
    const CURRENCY_SILVER_OUT = 'silver_out';
    const CURRENCY_GOLD_IN = 'gold_in';
    const CURRENCY_GOLD_OUT = 'gold_out';
    const CURRENCY_FACTORY_ONE = 'factory_one';
    const CURRENCY_FACTORY_TWO = 'factory_two';

public static function tableName()
{
    return '{{%user_balance}}';
}

public function rules()
{
    return [
        ['user_id', 'integer'],
    ];
}

public function attributeLabels()
{
    return [
        'silver_in' => \Yii::t('app', 'Серебро'),
        'silver_out' => \Yii::t('app', 'Серебро на вывод'),
        'gold_in' => \Yii::t('app', 'Золото'),
        'gold_out' => \Yii::t('app', 'Золото на вывод'),
        'factory_one' => \Yii::t('app', 'Фабрика 1'),
        'factory_two' => \Yii::t('app', 'Фабрика 2'),
    ];
}

public function getUser()
{
    return $this->hasOne(User::className(), ['id' => 'user_id']);
}

public function get($currency)
{
     $currentSum = \Yii::$app->db->createCommand('
        SELECT `'.$currency.'`
        FROM {{%user_balance}}
        WHERE `user_id` = :user_id', [
        ':user_id' => $this->user_id
     ])->queryScalar();
     return BalanceHelper::toDigits($currency, $currentSum);
}

public function has($currency, $sum)
{
    $currentSum = \Yii::$app->db->createCommand('
        SELECT `'.$currency.'`
        FROM {{%user_balance}}
        WHERE `user_id` = :user_id', [
        ':user_id' => $this->user_id
    ])->queryScalar();

    return $currentSum >= BalanceHelper::toCoins($currency, $sum);
}

public function decrease($currency, $sum)
{
    if ($this->has($currency, $sum)) {
        $sum = BalanceHelper::toCoins($currency, $sum);

        $rows = \Yii::$app->db->createCommand('
            UPDATE {{%user_balance}}
            SET `' . $currency . '` = `' . $currency . '` - :sum
            WHERE `' . $currency . '` >= :sum
            AND `user_id` = :user_id', [
            ':sum' => $sum,
            ':user_id' => $this->user_id
        ])->execute();

        return $rows > 0;
    }
    return false;
}

public function increase($currency, $sum)
{
    $sum = BalanceHelper::toCoins($currency, $sum);

    $rows = \Yii::$app->db->createCommand('
        UPDATE {{%user_balance}}
        SET `' . $currency . '` = `' . $currency . '` + :sum
        WHERE `user_id` = :user_id', [
    ':sum' => $sum,
    ':user_id' => $this->user_id
    ])->execute();

    return $rows > 0;
}

public static function getCurrencyArray()
{
    return [
        self::CURRENCY_SILVER_IN => \Yii::t('app', 'Серебро'),
        self::CURRENCY_SILVER_OUT => \Yii::t('app', 'Серебро на вывод'),
        self::CURRENCY_GOLD_IN => \Yii::t('app', 'Золото'),
        self::CURRENCY_GOLD_OUT => \Yii::t('app', 'Золото на вывод'),
        self::CURRENCY_FACTORY_ONE => \Yii::t('app', 'Фабрика 1'),
        self::CURRENCY_FACTORY_TWO => \Yii::t('app', 'Фабрика 2'),
    ];
}

public function getCurrencyString()
{
    $currencies = self::getCurrencyArray();
    return isset($currencies[$this->currency]) ? $currencies[$this->currency] : '';
}

}

这是显示错误的文件

main.php:

<?php if (\Yii::$app->user->isGuest) { ?>
<?php } else { ?>
<div class="main-p">
    <p><a href="#"><i class="fa fa-home" aria-hidden="true"></i>Серебро</a>
    <?= \Yii::$app->user->identity->balance->get(UserBalance::CURRENCY_SILVER_IN) ?>
    <p><a href="#"><i class="fa fa-home" aria-hidden="true"></i>Серебро на вывод</a></li>
    <?= \Yii::$app->user->identity->balance->get(UserBalance::CURRENCY_SILVER_OUT) ?>
    <p><a href="#"><i class="fa fa-home" aria-hidden="true"></i>Золото</a></li>
    <?= \Yii::$app->user->identity->balance->get(UserBalance::CURRENCY_GOLD_IN) ?>
    <p><a href="#"><i class="fa fa-home" aria-hidden="true"></i>Золото на вывод</a></li>
    <?= \Yii::$app->user->identity->balance->get(UserBalance::CURRENCY_GOLD_OUT) ?>
    <p><a href="#"><i class="fa fa-home" aria-hidden="true"></i>Фабрика 1</a></li>
    <?= \Yii::$app->user->identity->balance->get(UserBalance::CURRENCY_FACTORY_ONE) ?>
    <p><a href="#"><i class="fa fa-home" aria-hidden="true"></i>Фабрика 2</a></li>
    <?= \Yii::$app->user->identity->balance->get(UserBalance::CURRENCY_FACTORY_TWO) ?>
</div>
<?php } ?>

也许缺少某些东西?

0 个答案:

没有答案