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

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?

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

        $CI =& get_instance();

        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;

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


Any one else?

yuck... :-)

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

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

[eluser]Jeffrey Lasut[/eluser]
Take a look @ DMZ datamapper:

Will make your life a lot easier.

Maybe I am a bad programmer but I would do it this way Smile
Controller code
function debug($test)
    echo '<pre>';
    echo '</pre>';

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);

  return $result;

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.