Problems with models calling other models |
[eluser]swissbastian[/eluser]
Hi there I've got some problems with models and I can't figure out what mistakes I made. The browser tells me: Quote:A PHP Error was encountered dateim and verzeichnism are models, verzeichnis_titel is a function from verzeichnism. Line 308 of dateim.php: Code: $output = $this->verzeichnism->verzeichnis_titel($id); verzeichnis_titel(): Code: function verzeichnis_titel($id) Is it bad practice to call a model in another model? Thanks for any help, Bastian
[eluser]Clooner[/eluser]
[quote author="swissbastian" date="1239017715"]Is it bad practice to call a model in another model?[/quote] I would suggest that you create an my_model by extending the model and put the function which you are calling in multiple models in that one. When creating a model extend it from my_model. This way you won't have duplicate code and your code won't be messy. Hope this helps. There is an explanation on how to extend the models in the user guide.
[eluser]swissbastian[/eluser]
Okay, it's definitely a problem to call a function from another model. But I didn't find any explanation on how to extend models in the user guide.
[eluser]Colin Williams[/eluser]
Code: $ci =& get_instance();
[eluser]xwero[/eluser]
I think most of these other model method calls can be prevented by creating a join query in the method. It seems like a a lot of people push the one model one table rule too far.
[eluser]Clooner[/eluser]
http://ellislab.com/codeigniter/user-gui...aries.html has some thing about extending libraries. Basically what you want to do is create a model called my_model which is extended of model and put this in the libraries folder. Extend the model which you are working on off my_model and put the function which you call in all the models in my_model.
[eluser]Colin Williams[/eluser]
[quote author="xwero" date="1239026323"]I think most of these other model method calls can be prevented by creating a join query in the method. It seems like a a lot of people push the one model one table rule too far.[/quote] Agreed. But if someone feels they absolutely must, get an instance of the controller class with get_instance()
[eluser]xwero[/eluser]
I think if they really want to do it they should do it in the controller instead of obscuring the loading of the model inside the other model. Code: // model
[eluser]TheFuzzy0ne[/eluser]
I don't understand why CodeIgniter doesn't automatically load models into other models as it does with libraries. Is there any reason for this? |
Welcome Guest, Not a member yet? Register Sign In |