处理与ZF partialLoop的一对多关系

时间:2010-01-20 16:12:49

标签: php zend-framework zend-view

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

但是这个代码在artists / list-artists.phtml中:

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

我收到此错误:

  

致命错误:调用成员函数   findDependentRowset()在非对象

var_dump = $this->artist NULL

1 个答案:

答案 0 :(得分: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,则可能还需要在表类中设置一些内容。

相关问题