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.