2010-01-20 32 views
1

可以說我在列出音樂藝術家,每位藝術家都擁有存儲在藝術家表中的基本信息,如姓名,年齡等。他們還有專輯表(專輯名稱/專輯封面等)中的條目,使用藝術家ID作爲外鍵引用藝術家表。處理與ZF的一對多關係partialLoop

我有Model_Artist(Artist.php)文件:

class Model_Artist extends Zend_Db_Table_Abstract 
{ 
    protected $_name = 'artist'; 
    protected $_dependentTables = array('Model_ArtistAlbums'); 
    public function fetchArtistss() 
    { 
     $select = $this->select(); 
     return $this->fetchAll($select); 
    } 
} 

,並在Model_ArtistAlbums(ArtistAlbums.php)文件

class Model_ArtistAlbums extends Zend_Db_Table_Abstract 
{ 
    protected $_name = 'albums'; 

    protected $_referenceMap = array(
     'Artists' => array(
      'columns'  => 'alb_art_id', 
      'refTableClass' => 'Model_Artist', 
      'refColumns' => 'art_id' 
     ) 
    ); 
    // etc 
} 

在我的控制器:

public function indexAction() 
{ 
    /* THIS WORKS 
    $art = new Model_Artist(); 
    $artRowset = $art->find(1); 
    $art1 = $artRowset->current(); 
    $artalbums = $art1->findDependentRowset('Model_ArtistAlbums'); 
    foreach($artalbums as $album){ 
     echo $album->alb_title."<br>"; 
    } 
    */ 
    $arts = new Model_Artist(); 
    $this->view->artists = $arts->fetchArtists(); 
} 

在視圖文件中:

$this->partial()->setObjectKey('artist'); 
echo $this->partialLoop('admin/list-artists.phtml', $this->artists); 

但與此代碼中的藝術家/列表artists.phtml:

foreach($this->artist->findDependentRowset('albums') as $album): 
// other stuff 
endforeach; 

我得到這個錯誤:

Fatal error: Call to a member function findDependentRowset() on a non-object

$this->artist = NULL一個var_dump。 - 我beleive這種全球變化的對象鍵爲partialLoop幫手的情況下,所以你可能要選擇更多的東西一般比artist或記住,如果將其復位

<?php $this->partialLoop()->setObjectKey('artist'); ?> 
<?php echo $this->partialLoop('yourpartial.phtml', $artists); ?> 

注:

回答

4
在你調用視圖

您可以在同一視圖中稍後使用partialLoop。

,並在您的部分:

<?php foreach($this->artist->findDependentRowSet('Albums') as $album): ?> 
<!-- looping stuff here --> 
<?php endforeach; ?> 

檢查一個Zend_Db_Table_Row的文檔/ API的實際參數(縣)findDependentRowset應該在你的項目模型的命名約定的條件是什麼。如果您尚未定義_referenceMap,則可能還需要在表格類中設置一些內容。

+0

謝謝,會檢查出這 – robjmills

+0

奇怪的是,此刻我得到這個錯誤:「消息:按名稱查找插件'FindDependentRowset'沒有在註冊表中找到」 – robjmills

+0

哦OK $這必須引用視圖實例,在我的部分大「呃」看到編輯。 – prodigitalson