Welcome Guest, Not a member yet? Register   Sign In
Models and controllers classes member variables issues
#1

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


Messages In This Thread
Models and controllers classes member variables issues - by El Forum - 02-18-2010, 09:15 AM
Models and controllers classes member variables issues - by El Forum - 02-18-2010, 09:47 AM
Models and controllers classes member variables issues - by El Forum - 02-18-2010, 12:18 PM
Models and controllers classes member variables issues - by El Forum - 02-19-2010, 04:53 AM
Models and controllers classes member variables issues - by El Forum - 02-19-2010, 08:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB