[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:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
DEBUG - 2011-12-08 17:33:38 --> Config Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Hooks Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Utf8 Class Initialized
DEBUG - 2011-12-08 17:33:38 --> UTF-8 Support Enabled
DEBUG - 2011-12-08 17:33:38 --> URI Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Router Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Output Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Security Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Input Class Initialized
DEBUG - 2011-12-08 17:33:38 --> XSS Filtering completed
DEBUG - 2011-12-08 17:33:38 --> XSS Filtering completed
DEBUG - 2011-12-08 17:33:38 --> XSS Filtering completed
DEBUG - 2011-12-08 17:33:38 --> CRSF cookie Set
DEBUG - 2011-12-08 17:33:38 --> Global POST and COOKIE data sanitized
DEBUG - 2011-12-08 17:33:38 --> Language Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Loader Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Controller Class Initialized
DEBUG - 2011-12-08 17:33:38 --> Model Class Initialized <<<< 1st time
DEBUG - 2011-12-08 17:33:38 --> Model Class Initialized <<<< 2nd time
DEBUG - 2011-12-08 17:33:38 --> Final output sent to browser
DEBUG - 2011-12-08 17:33:38 --> 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!