CodeIgniter Forums
Accesning a model in a model (bad practice?) - 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: Accesning a model in a model (bad practice?) (/showthread.php?tid=23473)



Accesning a model in a model (bad practice?) - El Forum - 10-12-2009

[eluser]ennis[/eluser]
Hey guys I am pretty new to mvc and codeignitor.

I do understand the concepts of mvc etc.

I am wokring on a project with another programmer and he is accesing models from inside a model is this bad practice? should what is trying to do be achived through joins in sql or maybe binding data together from 2 different models in the controller?

Any tips or ressource links would be mutch appreciated Smile

Bellow code example where in the client_relation_model the code accesses 2 other models to get some data this just seems very messy too me?

Code:
function findCompanyReferences($company_id){    
        $sql = "SELECT * FROM client_relation WHERE client_id_parent = ?";
        $query  = $this->db->query($sql,array($company_id));

        $CI =& get_instance();
        $CI->load->model('database/clients_model','Clients_model',true);
        $CI->load->model('database/ticket_model','Ticket_model',true);        

        if($query->num_rows > 0){
            
            foreach($query->result() as $row){
                
                $data[] = array($CI->Clients_model->getClientFromID($row->client_id_child),
                                $CI->Ticket_model->getTicketFromClient($row->client_id_child) );

            }
        
            return $data;
        }
        $query->free_result();    
    }}



Accesning a model in a model (bad practice?) - El Forum - 10-12-2009

[eluser]Jeffrey Lasut[/eluser]
Fun documentation:

http://badprogrammer.infogami.com/


Accesning a model in a model (bad practice?) - El Forum - 10-13-2009

[eluser]ennis[/eluser]
Any one else?


Accesning a model in a model (bad practice?) - El Forum - 10-13-2009

[eluser]rogierb[/eluser]
yuck... :-)

I'm a firm believer of join(t)s.

Go for joins, much faster, less loading, compiling etc...


Accesning a model in a model (bad practice?) - El Forum - 10-13-2009

[eluser]Jeffrey Lasut[/eluser]
Take a look @ DMZ datamapper:
http://ellislab.com/forums/viewthread/112904/

Will make your life a lot easier.


Accesning a model in a model (bad practice?) - El Forum - 10-13-2009

[eluser]John_Betong[/eluser]
 
Maybe I am a bad programmer but I would do it this way Smile
 
 
Controller code
Code:
//=================================================
function debug($test)
{
    echo '<pre>';
        print_r($test);
    echo '</pre>';
    die;
}


//=================================================
function findCompanyReferences($company_id=NULL)
{    
  $query     = $this->clients_model->get_client_info($company_id);
  $result    = array();
    
    if($query->num_rows > 0)
    {
        foreach($query->result() as $row){

        $client    =    $this->clients_model->getClientFromID($row->client_id_child);
        // debug($client);
        
        $ticket    = $this->Ticket_model->getTicketFromClient($row->client_id_child);
        // debug($ticket);
        
        $result[] = array($client, $ticket);
        // debug($data);
        
      endforeach;  
    }
    $query->free_result();    

  return $result;
}//endif
&nbsp;
&nbsp;
&nbsp;


Accesning a model in a model (bad practice?) - El Forum - 10-13-2009

[eluser]ennis[/eluser]
Yeah thanks for all the replys, just went through some more code that ihavent done and well will be suing some joins and then calling some models in the controller..

Right now the code calls the clients model model that calls the cliens_relation model that then call the client model to get tclietns pair them with tickets from the ticket model then calls the tick model to get the indivudal ticks for the ticket all this is run ever time I call getClients hehe so yeah not good Smile

So will refacture and break it up so that tickets are only pulled out when needed.