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
.
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
.