Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
某个功能应该去哪里?模型还是控制器?CakePHP_Cakephp_Models - Fatal编程技术网

某个功能应该去哪里?模型还是控制器?CakePHP

某个功能应该去哪里?模型还是控制器?CakePHP,cakephp,models,Cakephp,Models,Trips有很多腿,也有很多段 在我的航班搜索应用程序中,我有一个返回唯一航段目的地的函数。此功能用于跳闸控制器方法。我是否将功能放在行程模型或腿部模型中?如果在leg模型中,我用$this->Trip->leg->findUniqueDests来调用它。。。。。?我这样问是因为我想坚持CakePHP惯例。谢谢 //code that finds ALL destinations for each Leg $destinations=$this->Trip->Leg->find

Trips有很多腿,也有很多段

在我的航班搜索应用程序中,我有一个返回唯一航段目的地的函数。此功能用于跳闸控制器方法。我是否将功能放在行程模型或腿部模型中?如果在leg模型中,我用$this->Trip->leg->findUniqueDests来调用它。。。。。?我这样问是因为我想坚持CakePHP惯例。谢谢

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

}

是的,您可以将其放在腿部模型中。这将允许您从任何其他相关模型调用该方法:

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

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

// Segment Controller
$this->Segment->Leg->findUniqueDests($destinations);
感谢您知道它应该在模型中。许多从CakePHP开始的人将他们所有的方法都塞进了控制器中

以这种方式在模型中执行此操作允许您在整个应用程序中重复使用代码。实际上,这种效用函数可以放在任何模型中。但是,由于它处理的是腿部,因此最符合逻辑的是腿部模型

问题:为什么每次将目标添加到数组时都要进行排序?这将更加优化:

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

多条航段,每条航段多个航段,飞行。。。你的应用程序能控制飞行的蜈蚣吗我认为这是模型中最好的。我们收集了Kayak目前提供的所有数据。同意有些可能是多余的。胖型瘦控制器,它经常帮助我检查我的代码:)你是对的!谢谢我借用了这个函数的大部分,所以没有充分考虑/使用它。