2010-06-07 57 views
4

我一直在尋找教程,以更好地理解這一點,但我沒有運氣。請原諒漫長的探索,但我想確保我解釋自己。如何通過控制器調用模型中的方法? Zend Framework

首先,我對MVC結構很陌生,儘管我一直在做教程和盡我所能學習。

我一直在將現場網站轉移到Zend Framework模型中。到目前爲止,我擁有views/scripts/index/example.phtml中的所有視圖。

因此,因此我使用一個索引控制器和我在每個頁面的每個動作方法的代碼:IE public function exampleAction()

因爲我不知道如何與模型交互,我把所有的方法在控制器的底部(一個胖控制器)。

所以基本上,我有一個工作站點通過使用視圖和控制器,沒有模型。

...

現在我正試圖學習如何合併模型。

所以我創建的視圖在:

view/scripts/calendar/index.phtml 

我創建了一個新的控制器:

controller/CalendarControllers.php 

,並在一個新的模式:

model/Calendar.php 

的問題是,我認爲我與模特溝通不正確(我仍然是OOP的新手)。

你可以看看我的控制器和型號,並告訴我,如果你看到一個問題。

我需要從runCalendarScript()返回一個數組,但我不確定是否可以返回一個數組到我想要的對象?我真的不知道如何從控制器「運行」runCalendarScript()?

感謝您的幫助!我剝出最多的方法膽量爲簡潔起見:

控制器:

<?php 

class CalendarController extends Zend_Controller_Action 
{ 

    public function indexAction() 
    { 
     $finishedFeedArray = new Application_Model_Calendar(); 

    $this->view->googleArray = $finishedFeedArray; 
    } 
} 

型號:

<?php 

    class Application_Model_Calendar 
    { 

public function _runCalendarScript(){ 
    $gcal = $this->_validateCalendarConnection(); 
    $uncleanedFeedArray = $this->_getCalendarFeed($gcal); 
    $finishedFeedArray = $this->_cleanFeed($uncleanedFeedArray); 

    return $finishedFeedArray; 

} 


    //Validate Google Calendar connection 
    public function _validateCalendarConnection() 
    { 
    ... 
     return $gcal; 
    } 


    //extracts googles calendar object into the $feed object 
    public function _getCalendarFeed($gcal) 
    { 
    ... 
    return $feed; 
    } 

    //cleans the feed to just text, etc 
    protected function _cleanFeed($uncleanedFeedArray) 
    { 
$contentText = $this->_cleanupText($event); 
     $eventData = $this->_filterEventDetails($contentText); 

return $cleanedArray; 
    } 

    //Cleans up all formatting of text from Calendar feed 
    public function _cleanupText($event) 
    { 
... 
return $contentText; 
     } 



     //filterEventDetails 
     protected function _filterEventDetails($contentText) 
     { 
      ... 
      return $data; 
} 
    } 

編輯:對不起,我不知道爲什麼我在代碼上的格式看起來很醜陋...

回答

2

Joel, 所以你把整個模型對象放到一個名爲$ finishedFeedArray的變量中,這會變得混亂(它是不是數組,它是一個對象)。

我認爲這就是你的問題所在。然後,您試圖將這個變量賦予您的視圖,我假定在您的視圖中顯示值。在你的視圖腳本中,任何試圖像數組一樣處理這個變量都會導致問題。

試試這個:

class CalendarController extends Zend_Controller_Action 
{ 

    public function indexAction() 
    { 
     $calendar = new Application_Model_Calendar(); 

     $this->view->googleArray = $calendar->_runCalendarScript(); 
    } 
} 

有一個輕微的風格問題,還有......我不會名稱的公共職能爲下劃線作爲第一個字符。否則,這種改變至少應該會導致你的代碼出現一個錯誤。

+0

太棒了!非常感謝 - 真正教會了我如何將這一切融合在一起的重要教訓! – Joel 2010-06-07 04:21:02