CodeIgniter Forums
Multiple calls to model in controller fails - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Multiple calls to model in controller fails (/thread-36511.html)



Multiple calls to model in controller fails - El Forum - 12-04-2010

[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;
    }



Multiple calls to model in controller fails - El Forum - 12-04-2010

[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


Multiple calls to model in controller fails - El Forum - 12-04-2010

[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


Multiple calls to model in controller fails - El Forum - 12-06-2010

[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


Multiple calls to model in controller fails - El Forum - 12-06-2010

[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?