Models and controllers classes member variables issues |
[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 ... and this is the output ... Code: Hello from foo 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.
[eluser]danmontgomery[/eluser]
You really shouldn't be directly inserting the model using $this, if you're looking to directly relate models to database tables you should look at an ORM like DMZ or Doctrine.
[eluser]JoostV[/eluser]
Prepend your class variables with an underscore to make them private. See userguide Code: var $_fooInMyModel = ''; Or, just use good old PHP5 ![]() Code: private $_fooInMyModel = '';
[eluser]Unknown[/eluser]
![]() Code: INSERT INTO `foo_table` (`_fooInMyModel`, `fooInFoo`) VALUES ('Hello, from modelfoo!', 'Hello from foo')
[eluser]danmontgomery[/eluser]
Prepending variables with _ doesn't make them private. It's used as an indicator in PHP4 that they should be considered private variables, since PHP4 doesn't support public/private/protected. This will happen with any variables declared before the model is loaded, when any model is loaded it calls _assign_libraries. Code: function _assign_libraries($use_reference = TRUE) Again, you shouldn't be directly inserting a model without checking the fields you're inserting. You should really be using an ORM like DMZ or Doctrine. |
Welcome Guest, Not a member yet? Register Sign In |