Welcome Guest, Not a member yet? Register   Sign In
Problem with construct double loading
#1

Hi

CodeIgniter noob here. I have an interesting problem with CodeIgniter. My model constructs are always always loaded at least twice.

I tested the system with fresh CI install. Unziped codeigniter_3.0.3.zip to server (Ubuntu 14.04 LTS), and created simple model:

PHP Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class 
Test_model extends CI_Model
{
 
   function __construct()
 
   {
 
       echo "Construct loaded<br />";
 
       log_message('debug'"Test_model loaded...");
 
   }


And then added it to default Welcome controller:

PHP Code:
<?php

defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {

    public function 
index()
    {
        
$this->load->model('Test_model');
        
$test = new Test_model();
        
$this->load->view('welcome_message');
    }


And changed the log threshold to 4. And that prints the line "Construct loaded" twice to screen and prints three "Test_model loaded..." messages to log. I found that it might be cause by the favicon.ico file not found, so I added it, but no change.

So, any pointers how to rid of this problem? Or is this actually problem at all (1)? Is this a (known?) bug in CI, or problem with server? Or problem with configuration? Or have I just misunderstood something fundamental?

I can replicate this behavior with all linux servers I have access to.

Server: Ubuntu linux 14.04 LTS (with latest updates and PHP 5.5.9), or webhotel server (with some debian and PHP 5.3.27).
Client: Os X (updated yosemite or El capitan, & latest Firefox or Chrome)

(1) I like to to setup things in construct, so if this is working as intented or no fix possible then I have to change my coding style (and rationalize it to rest of team). Also, software I'm working with uses some DB interaction which was planned to do in construct, but if the double construct execution is just fact of life, we have to change it, because construct double execution seems to double our DB server load.

Anyway. Thank you for reading.
Reply
#2

Remove the line `$test = new Test_model();`.

This is because `$this->load->model('Test_model')` creates a Test_model instance (and set it to $this->Test_model).
Reply
#3

(12-06-2015, 02:41 PM)kenjis Wrote: Remove the line `$test = new Test_model();`.

This is because `$this->load->model('Test_model')` creates a Test_model instance (and set it to $this->Test_model).

Whoa. How did I miss this... Thank you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB