Welcome Guest, Not a member yet? Register   Sign In
Model loaded twice with child property empty on the first load. Why?
#1

[eluser]Unknown[/eluser]
Hi,
I'm quite new to CI but old to PHP.

I'm a little confused with extending a model and was wondering if someone could point me in the right direction.

I have core/MY_Model.php that extends CI_Model.
MY_Model.php has a protected property named $_table declared.

I have models/example_model.php that extends MY_Model.
example_model.php has a protected property named $_table that contains the string 'examples'.

When my controller is instantiated, it loads example_model in the constructor.
It seems my model is being loaded twice, the first time around, $this->_table is empty but gets populated the second time it's loaded.

This isn't normal and I was wondering what I'm doing wrong here.

All my code is below.

controllers/examples.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Examples extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('example_model', 'example');
    }

    public function index() { }
}

models/example_model.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Example_Model extends MY_Model
{
    protected $_table = 'examples';

    public function __construct()
    {
        parent::__construct();
    }
}

core/MY_Model.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    function display($show, $info = NULL,
        $style = 'style="border: 1px solid #000; background: #444; color: #fff;
            padding: 1em; margin: 0.5em;"')
    {
        echo "
            <div $style>
                <code>$info #</code>
                <pre>" . htmlspecialchars(print_r($show, true)) . '</pre>
            </div>';
    }

class MY_Model extends CI_Model
{
    protected $_table;

    public function __construct()
    {
        parent::__construct();
        echo display($this->_table, __LINE__);
        $this->example();
    }

    public function example()
    {
        echo display($this->_table, __LINE__);
    }

}


My log file:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?&gt;

DEBUG - 2011-12-08 17:33:38 --&gt; Config Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Hooks Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Utf8 Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; UTF-8 Support Enabled
DEBUG - 2011-12-08 17:33:38 --&gt; URI Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Router Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Output Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Security Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Input Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; XSS Filtering completed
DEBUG - 2011-12-08 17:33:38 --&gt; XSS Filtering completed
DEBUG - 2011-12-08 17:33:38 --&gt; XSS Filtering completed
DEBUG - 2011-12-08 17:33:38 --&gt; CRSF cookie Set
DEBUG - 2011-12-08 17:33:38 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2011-12-08 17:33:38 --&gt; Language Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Loader Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Controller Class Initialized
DEBUG - 2011-12-08 17:33:38 --&gt; Model Class Initialized    <<<< 1st time
DEBUG - 2011-12-08 17:33:38 --&gt; Model Class Initialized    <<<< 2nd time
DEBUG - 2011-12-08 17:33:38 --&gt; Final output sent to browser
DEBUG - 2011-12-08 17:33:38 --&gt; Total execution time: 0.0532


With the above code, I see:
Code:
21 #

27 #

21 #
examples

27 #
examples

So, only on the second round does my property become populated. But why is it being loaded twice, to begin with?

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB