Where should a certain function go- model or controller? CakePHP -


trips hasmany legs hasmany segments

in flight search app, i've got function returns unique leg.destination(s). function trip controller method. put function in trip model or leg model? if in leg model call $this->trip->leg->finduniquedests.....? i'm asking because want stick cakephp convention. thanks!

//code finds destinations each leg $destinations=$this->trip->leg->find('all', array('limit'=>100,'fields'=>'leg.destination'));  //code finds unique destinations (to used search flights particular city function finduniquedests($destinations){   $unique_destinations = array();   foreach ($destinations $dest)   {       if(!in_array($dest, $unique_destinations))       {           $unique_destinations[] = $dest;           sort($unique_destinations);       }   }   return $unique_destinations; 

}

yes, put in leg model. allow call method other related model:

// trip controller $this->trip->leg->finduniquedests($destinations);  // leg controller $this->leg->finduniquedests($destinations);  // segment controller $this->segment->leg->finduniquedests($destinations); 

kudos knowing should in model. many people starting cakephp cram of methods in controllers.

doing in model way allows re-use code on application. in reality, kind of utility function placed in model. since dealing legs, logical home leg model.

question: why sorting every time destination added array? more optimized:

function finduniquedests($destinations) {     $unique_destinations = array();     foreach ($destinations $dest) {         if(!in_array($dest, $unique_destinations)) {             $unique_destinations[] = $dest;         }     }     return sort($unique_destinations); } 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -