Where should the specific go-model or controller function be? Cakephp

Rides have many legs with multiple segments

In my in-flight search app, I have a function that returns unique Leg.destination (s). This function is for the shutdown controller method. Do I put a function in a ride model or foot model? If in the foot model I call it using $ this-> Trip-> Leg-> findUniqueDests .....? I ask because I want to stick to the CakePHP convention. Thank!

//code that finds ALL destinations for each Leg
$destinations=$this->Trip->Leg->find('all', array('limit'=>100,'fields'=>'Leg.destination'));

//code that finds the unique destinations (to be used to search all flights for a particular city
function findUniqueDests($destinations){
  $unique_destinations = array();
  foreach ($destinations as $dest)
  {
      if(!in_array($dest, $unique_destinations))
      {
          $unique_destinations[] = $dest;
          sort($unique_destinations);
      }
  }
  return $unique_destinations;

}

+3
source share
1 answer

Yes, you would put it in a Leg model. This will allow you to call a method from any other related model:

// Trip Controller
$this->Trip->Leg->findUniqueDests($destinations);

// Leg Controller
$this->Leg->findUniqueDests($destinations);

// Segment Controller
$this->Segment->Leg->findUniqueDests($destinations);

, , . , CakePHP, .

. , . , Leg.

: , ? :

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

Source: https://habr.com/ru/post/1778195/


All Articles