2010-12-02 4 views
3

Trips hasMany Leg has hasMany Segment特定の機能はどこに行くべきですか? CakePHP

私のフライト検索アプリでは、ユニークなLeg.destination(s)を返す関数があります。この機能はトリップコントローラ方式用です。この機能をトリップモデルまたはレッグモデルに入れますか?脚モデルの場合、私は$ 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; 

}

+0

多くの足、足あたり多くのセグメント、フライト...あなたのアプリが飛ぶムカデを制御しますか? :P私はこれがモデルで一番良いと思います。 – FrustratedWithFormsDesigner

+0

カヤックが提供しているすべてのデータを引き出します。いくつかは過剰かもしれないと合意した。 – JohnAllen

+0

ファットモデルスキニーコントローラ、それはしばしば私のコードをチェックしておくのに役立ちます:) – serialk

答えて

3

はい、あなたは脚モデルにそれを置くでしょう。これにより、他の関連するモデルからこのメソッドを呼び出すことができます。

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

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

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

モデルに含めるべきであることを知っています。 CakePHPで始まる多くの人々は、コントローラ内のすべてのメソッドを詰め込みます。

このようにモデルで行うと、アプリケーション全体でコードを再利用できます。実際には、この種のユーティリティ機能はどのモデルにも置くことができます。しかし、それはLegsを扱っているので、最も論理的な家はLegモデルになります。

質問:宛先が配列に追加されるたびに並べ替えるのはなぜですか?これはもっと最適化されます:

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

あなたは正しいです!ありがとう。私はその機能のほとんどを借りていたので、それについて完全には考えていなかった/それを使いました。 – JohnAllen

関連する問題