[eluser]Unknown[/eluser]
Hi, I'm having a strange behaviour using a model within a controller, I'm not much familiar with CI development, I just started from a few weeks, anyway, my problem is that model instance somehow considers the variables declared in the controller class as member class of his own instance and then it tries to produce SQL code according to this, needless to say this cause an error since my model table doesn't contain this row.
As example:
Code:
class Foo extends Controller
{
var $fooInFoo = '';
function Foo()
{
parent::Controller();
$this->fooInFoo = "Hello from foo";
}
function index()
{
echo "<pre>";
$this->load->model('MyModel');
echo $this->fooInFoo;
echo "\n";
echo $this->MyModel->fooInMyModel;
echo "\n";
print_r($this->MyModel);
echo "</pre>";
}
}
class MyModel extends Model
{
var $fooInMyModel = '';
function MyModel()
{
parent::Model();
$this->fooInMyModel = "Hello, from modelfoo!";
}
function insert()
{
$this->db->insert('foo_table', $this);
}
}
... and this is the output ...
Code:
Hello from foo
Hello, from modelfoo!
MyModel Object
(
[fooInMyModel] => Hello, from modelfoo!
[_parent_name] => MyModel
[fooInFoo] => Hello from foo
[_ci_scaffolding] =>
[_ci_scaff_table] =>
[config] => CI_Config Object
(
.... etc ....
If I declare controller member variable as private this don't happen, btw this is not compatible with PHP < 5.
Is this considered normal or am I taking a wrong direction using models and controller?
thx for the replies.