CodeIgniter Forums
Problems with models calling other models - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problems with models calling other models (/showthread.php?tid=17468)



Problems with models calling other models - El Forum - 04-06-2009

[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

Severity: Notice

Message: Undefined property: Dateim::$verzeichnism

Filename: models/dateim.php

Line Number: 308


Fatal error: Call to a member function verzeichnis_titel() on a non-object in /foo/bar/public_html/system/application/models/dateim.php on line 308

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)
        {
            $query = $this->db->get_where('verzeichnisse', array('id' => $id));
            $zeile = $query->row();
            
            return $titel = $zeile->titel;
            
        }

Is it bad practice to call a model in another model?

Thanks for any help,
Bastian


Problems with models calling other models - El Forum - 04-06-2009

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


Problems with models calling other models - El Forum - 04-06-2009

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


Problems with models calling other models - El Forum - 04-06-2009

[eluser]Colin Williams[/eluser]
Code:
$ci =& get_instance();
$ci->load->model('other_model');
$ci->other_model->method();



Problems with models calling other models - El Forum - 04-06-2009

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


Problems with models calling other models - El Forum - 04-06-2009

[eluser]Clooner[/eluser]
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.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.


Problems with models calling other models - El Forum - 04-06-2009

[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()


Problems with models calling other models - El Forum - 04-06-2009

[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
function method()
{
    $ci =& get_instance();
    $ci->load->model('other_model');
    $output = $ci->other_model->method();
    // more output
    return $output;
}
// controller
function method
$this->load->model(array('model_one','model_two'));
$data['view'] = $this->model_one->method($this->model_two->method());
If they see then how much code it needs they will look for other solutions like the joined query.


Problems with models calling other models - El Forum - 04-06-2009

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