• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accesning a model in a model (bad practice?)

#1
[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();    
    }}

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

http://badprogrammer.infogami.com/

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

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

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

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

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

Will make your life a lot easier.

#6
[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;

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.