使用 ZFpartialLoop 处理一对多关系
假设我列出了音乐艺术家,每个艺术家都有基本信息,如姓名、年龄等,存储在艺术家表中。他们还在专辑表中拥有条目(专辑名称/专辑封面等),使用艺术家 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您调用视图时:
注意 - 我相信这会全局更改partialLoop帮助器实例的对象键,因此您可能想要选择比
artist
更通用的东西,或者如果您使用partialLoop,请记住重置它稍后以相同的视图进行其他操作。在您的部分中:
检查 Zend_Db_Table_Row 的 docs/api,了解
findDependentRowset
的实际参数应该根据您的项目模型命名约定。如果您尚未定义_referenceMap
,您可能还需要在表类中设置一些内容。in you calling view:
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:
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
.