Handling one-to-many relationship with ZF partialLoop
Posted
by snaken
on Stack Overflow
See other posts from Stack Overflow
or by snaken
Published on 2010-01-20T16:12:48Z
Indexed on
2010/03/15
6:49 UTC
Read the original article
Hit count: 436
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
.
© Stack Overflow or respective owner