使用 ZFpartialLoop 处理一对多关系

发布于 2024-08-19 06:26:23 字数 1819 浏览 13 评论 0原文

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

但是在 artss/list-artists.phtml 中使用此代码:

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

我收到此错误:

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

findDependentRowset() 位于$this->artist = NULL

Lets say i'm listing musical artists, each artist has basic information like Name, Age etc. stored in an artist table. They also have entries in an Albums table (album name/album cover etc), referencing the artist table using the artist id as a foreign key.

I have the Model_Artist (Artist.php) file:

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);
    }
}

and to the Model_ArtistAlbums (ArtistAlbums.php) file

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
}

in my controller:

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();
}

in the view file:

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

but with this code in artists/list-artists.phtml:

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

i get this error:

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

A var_dump of $this->artist = NULL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-08-26 06:26:23

在您调用视图时:

<?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,您可能还需要在表类中设置一些内容。

in you calling view:

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

Note - i beleive this changes the object key globally for the instance of the partialLoop helper so you might want to pick something more generic than artist or remember to reset it if you use partialLoop for something else later in the same view.

and in your partial:

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

Check the docs/api for Zend_Db_Table_Row for what the actual argument(s) to findDependentRowset should be in terms of your projects model naming conventions. You may also need to set some things up in your table class if you havent already defined the _referenceMap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文