1

假设我正在列出音乐艺术家,每个艺术家都有存储在艺术家表中的基本信息,如姓名、年龄等。它们在专辑表中也有条目(专辑名称/专辑封面等),使用艺术家 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

4

1 回答 1

4

在您调用视图中:

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

注意 - 我相信这会全局更改 partialLoop 帮助器实例的对象键,artist因此如果您稍后在同一视图中将 partialLoop 用于其他内容,您可能想要选择更通用的东西或记住重置它。

在你的部分:

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

根据您的项目模型命名约定,检查 Zend_Db_Table_Row 的 docs/api 的实际参数findDependentRowset应该是什么。如果您还没有定义_referenceMap.

于 2010-01-20T16:16:08.183 回答