2010-12-14 15 views
0

我正在開發一個網站;用戶可以使用一個註冊賬戶添加汽車和手機。不過,我在右側和左側的框中選擇了頂級命中(汽車,手機),而在中間我有最近添加的汽車和手機。我將頂級點擊分解爲元素,元素是topCars,topMobile。我想寫兩個函數來做我的東西。這兩個函數都應該是通用的,因爲將來我們可能會添加一些其他服務,如軟件或電影。然而,第一個功能將獲得最新的汽車或手機等,並將發佈在我的主頁中間。第二個功能應該是(汽車,手機,電影等)的頂級命中。我寫了下面的代碼:如何在我的元素中調用app_controller中的函數

class AppController extends Controller 
{ 
    var $uses = array('Car','Mobile'); 
    function Controler() 
    { 
    App::import('Core', 'Sanitize'); 
    } 

    function beforeRender(){ 
    $this->getLatestData(); 
    } 

    function beforeFilter() { 
    $this->getTopData(); 
    } 

    function getLatestData(){ 
    $latestCars = $this->Car->find('all', array('order' => array('id' => 'desc'), 
                'limit' => 7)); 
    $latestMobiles = $this->Mobile->find('all', array('order' => array('id' => 'desc'), 
                'limit' => 7)); 
    $this->set('cars',$latestCars); 
    $this->set('mobiles',$latestMobiles); 
    } 


    function getTopData($item){ // to get the top hits 

    $top = $this->$item->find('all', array('order' => array('hits' => 'asc'), 
               'limit' => 3)); 
    $this->set($item,$top); 
    } 
} 

我的代碼工作正常,最新添加的項目很好,但是當我試圖讓排名靠前則返回錯誤。 請幫我知道如何使它工作 謝謝

+1

什麼是錯誤?! – deceze 2010-12-15 02:08:31

回答

0

我想你不應該把這些方法放在app_controller。你應該製作另一個控制器,並把getLatestData和getTopData放在那裏。然後從元素使用$ this-> requestAction從視圖調用控制器動作。當然,你也應該使用某種緩存來加快速度。

有人會說,你不應該使用requestAction但這裏是從的蛋糕開發商之一的一篇文章: http://mark-story.com/posts/view/how-using-requestaction-increased-performance-on-my-site

1
$this->requestAction('App/getTopData/'.$param); 
+0

我編輯了你的帖子,看起來像代碼。你可以添加一些解釋它爲什麼這樣工作嗎?這裏通常不鼓勵使用單行,僅限代碼的帖子。 – Maerlyn 2013-05-19 16:32:55

相關問題