dqm83011 2016-04-24 06:32
浏览 174

Yii-如果我在保存之前返回false,则无法从beforesave()调用的函数更新记录

While saving a user i am updating the state of a record in my preference table from beforesave() of user table by calling a licensingObject() method of my SbLicensingbehavior. Now if I return true from the beforsave() function then preference table record gets updated. If I return false then the preference table record is not updated in data base.

user model code:

public function behaviors()
{
    return array(
        'behaviour_model_download' => array(
            'class' => 'application.components.SbLicensingBehavior'
        )
    );
}
public function beforeSave()
{
    $error = $this->licensingObject('user_count','save');
    if($error){
        return true;
    }
    return true;
}

Licensing behavior code:

<?php

class SbLicensingBehavior extends CBehavior {

/**
 * This function will receive the variable as parameter who's current state need to be
 * incremented or decremented based on operation parameter.
 * value send as parameter will be used as amount by which we need to increment our
 * current state variable, it will be usefull incase like we have a limitation on size
 * of a repo.     
 */

public function updateCurrentState($variable,$operation,$value = null)
{
    $preferenceMode = Preference::model()->findByAttributes(array(
        'variable' => $variable,
        'type' => 'system_limit',
    ));
    if(!$preferenceMode){
        return 'not found';
    }
    $currentStateVariable = "current_state_".$variable;

    $currentStatePreferenceModel = Preference::model()->findByAttributes(array(
        'variable' => $currentStateVariable,  
        'type' => 'system_limit'
    ));

    if ($operation == 'save'){
        $currentStatePreferenceModel->value += ($value == null?1:$value);
        if($preferenceMode->value < $currentStatePreferenceModel->value){
            $error = $this->updateFlagState($variable,1);
            return $error;
        }

    }

    if(!$currentStatePreferenceModel->save()){
        return 'Licensing variable can not be updated';
    }
    return $error;
}

/**
 * This function updates the notification variable value.
 */
public function updateFlagState($variable,$value)
{
    $prefrenceNotificationModel = Preference::model()->findByAttributes(array(
        'variable' => 'notification_'.$variable,  
        'type' => 'system_limit'
    ));
    if(!$prefrenceNotificationModel){
        return 'Licensing variable can not be updated'; 
    }
    $prefrenceNotificationModel->value = $value;
    $prefrenceNotificationModel->updated = time();
    if(!$prefrenceNotificationModel->save()) {
        return 'Licensing variable can not be updated';
    }
    return 'done';
}

public function licensingObject($variable,$operation=null,$value=null)
{
    switch ($variable) {
        case "user_count":

                $error = $this->updateCurrentState($variable,$operation,$value);
                return $error;
                if($error == 'done'){
                    return "user count has exceded the licensing limit, user can not be created";       
                }
                break;
            default:
        }
}

}

I am not getting what i am doing wrong.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python3.5 相关代码写作
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)