views:

190

answers:

2

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.

+3  A: 

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.

prodigitalson
thanks, will check this out
seengee
strange, at the moment i'm getting this error: "Message: Plugin by name 'FindDependentRowset' was not found in the registry"
seengee
oh ok $this must refer to the view instance, big "duh" on my part see edit.
prodigitalson
pretty confused but powering through :). should it not be <?php echo $this->partialLoop('yourpartial.phtml', $this->artists); ?> ? otherwise i get a foreach error
seengee
absolutely correct. That was my mistake... AGAIN. i dont knwo whether im helping or hurting! ;-)
prodigitalson
haha, you're definitely helping. thanks :)right okay, that now gives me this:Fatal error: Call to a member function findDependentRowset() on a non-object - thrown by this line: foreach($this->artist->findDependentRowset. what is the string "artist" in ->setObjectKey('artist') referencing specifically? not sure where i'm going wrong.
seengee
`setObjectKey` should tell it to expose the iteration of whatever var youre passing in to `partialLoop` (ie `$this->artists`) as that property within the `partialLoop` view. so for example `$this->partialLoop()->setObjectKey('model')` should expose the `$this->model` in each iteration of the view template in the loop.
prodigitalson
ah, so the label itself in that context is not significant. it could just as well be:$this->partialLoop()->setObjectKey('foobar'); and that would make $this->foobar ?
seengee
Thats is my understanding :-)
prodigitalson
just realised what it is, not sure if you changed it at some point or i just copied it wrong but i had $this->partial()->setObjectKey('artist') instead of $this->partialLoop()->setObjectKey('artist');
seengee
A: 

I have the same problem to list some parent or child table object in the another view just like above if anyone solve it please let me know me thanks.

Behrang