假设我正在列出音乐艺术家,每个艺术家都有存储在艺术家表中的基本信息,如姓名、年龄等。它们在专辑表中也有条目(专辑名称/专辑封面等),使用艺术家 ID 作为外键引用艺术家表。
我有 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);
但是在艺术家/list-artists.phtml 中有这段代码:
foreach($this->artist->findDependentRowset('albums') as $album):
// other stuff
endforeach;
我收到此错误:
致命错误:在非对象上调用成员函数 findDependentRowset()
Avar_dump
的$this->artist
= NULL
。