Welcome Guest, Not a member yet? Register   Sign In
Multiple calls to model in controller fails
#1

[eluser]novice32[/eluser]
Not sure why this happens, but if I run the below, it fails (specifically $rs_client should not be null). But if, say, I comment out
Code:
$this->Client_model->RemoveTrial($client_id);
then, it executes successfully ($rs_client is not null).

Are there constraints with calling/referencing a model in subsequent calls within a single Controller class??

/***** CONTROLLER *****/
Code:
function start() {

        $this->load->model('Client_model', '', TRUE);
        $rs =  $this->Client_model->GetActiveClients();

        foreach ($rs as $client_rec) {

            $client_id = $client_rec['ClientID'];
            $plan_start_date = $client_rec['ClientPlanStartDate'];
            $trial_end_date = $client_rec['ClientTrialEndDate'];

            //convert date strings to time for comparison
            $trial_end_date = strtotime($trial_end_date);
            $current_date = strtotime(date("Y-m-d"));

            $trial_start_next_day = strtotime($client_rec['ClientTrialStartDate']." +1 day");
           if ($current_date == $trial_end_date) {
                 $this->_email_user_trial_expired($client_id);
                 $this->Client_model->RemoveTrial($client_id);
              
             } elseif ($current_date == $trial_start_next_day) {                
                $this->_email_user_welcome_nextday($client_id);

            }
        }
    }  

   function _email_user_trial_expired($client_id = NULL) {
      
       if ($client_id == NULL) {
            return false;
        }

        $rs_client =  $this->Client_model->GetClientByID($client_id);  
        ....
    }
    
     function _email_user_welcome_nextday($client_id = NULL) {
        
        if ($client_id == NULL) {
            return false;
        }

        $this->load->model('Client_model', '', TRUE);      
        $rs_client = $this->Client_model->GetClientByID($client_id);  

        if (!$rs_client) {
            echo 'rs_client is empty!<br/>';
        }
        else {
            echo 'rs_client IS NOT empty!<br/>';
        }
        return;
    }
#2

[eluser]InsiteFX[/eluser]
Code:
function start() {

        $this->load->model('Client_model', '', TRUE);

        // you are get your records here!
        $rs =  $this->Client_model->GetActiveClients();

        foreach ($rs as $client_rec) {

            $client_id = $client_rec['ClientID'];
            $plan_start_date = $client_rec['ClientPlanStartDate'];
            $trial_end_date = $client_rec['ClientTrialEndDate'];

            //convert date strings to time for comparison
            $trial_end_date = strtotime($trial_end_date);
            $current_date = strtotime(date("Y-m-d"));

            $trial_start_next_day = strtotime($client_rec['ClientTrialStartDate']." +1 day");
           if ($current_date == $trial_end_date) {
                 $this->_email_user_trial_expired($client_id);

                 // you need to update your $rs records!
                 // after you run this do a redirect back to start
                 // to reload your $rs records.
                 $this->Client_model->RemoveTrial($client_id);
              
             } elseif ($current_date == $trial_start_next_day) {                
                $this->_email_user_welcome_nextday($client_id);

            }
        }
    }

InsiteFX
#3

[eluser]novice32[/eluser]
InsiteFX, thanks for responding. You added the following comments:
Code:
// you need to update your $rs records!
// after you run this do a redirect back to start
// to reload your $rs records.

I'm not entirely clear on what I'm doing wrong. You said to "update your $rs records". I'm only using $rs to identify candidate records, and relying on the private functions to perform the updates (_email_user_trial_expired and _email_user_welcome_nextday)

Thanks
Novice32
#4

[eluser]InsiteFX[/eluser]
Yo9u are updating your $rs record $this->Client_model->RemoveTrial($client_id);

But you are in a for each loop so the $rs doe's not contain the updated record.

You need to reload the $rs to get the updated record!

InsiteFX
#5

[eluser]novice32[/eluser]
Thanks . I will give it a shot. I'm sure that's the problem. By the way, how do you reload a result set?




Theme © iAndrew 2016 - Forum software by © MyBB