Zend Framework model relationships and accessing related records

916 views Asked by At

I've got a zend framework model:

class User extends Zend_Db_Table_Abstract {
    protected $_name    = 'users';
    protected $_primary = 'id';
    protected $_dependentTables = array('UserItem');

    public function refresh($) {
        $items = $this->findDependentRowset('UserItem', 'items');
            // do stuff with each item
        print_r($items);
        die();
    }
}

I've also got the related model:

<?php
class UserItem extends Zend_Db_Table_Abstract
{
    protected $_name = 'user_items';
    protected $_referenceMap    = array(
        'items' => array(
            // user_id is the name of the field on the USER_ITEMS table
            'columns'           => 'user_id',
            'refTableClass'     => 'User',
            // id is the name of the field on the USERS table
            'refColumns'        => 'id'
        )
    );
}

?>

I'd like to me able to call User->refresh(); and have a fancy little stack of things happen. But the error is

 Fatal error: Call to undefined method FbUser::findDependentRowset() 

Which is telling me that although I think i'm doing it right according to the Zend documentation http://framework.zend.com/manual/en/zend.db.table.relationships.html I'm missing something.

If it makes a difference, at first run the items list will be empty, then I'll "Upsert" a whole bunch of items - future runs I'll compare all items an only update the ones that are different. Hmm... nope that's definitely not relevant :)

1

There are 1 answers

1
prodigitalson On BEST ANSWER

You have your classses mixed. You should have 2 classes for every entity... a EntityTable (your table gateway) and an Entity (your row gateway). so the class declarations should look something like:

class User extends Zend_Db_Table_Row

class FbUser extends User

class UserTable extends Zend_Db_Table_Abstract

class UserItem extends Zend_Db_Table_Row

class UserItemTable extends Zend_Db_Table_Abstract

The row classes are your models (or are linked to models depending on how you want to wire it up), not the table classes.

The findDependentRowset method is on the Zend_Db_Table_Row class which is why you are getting the error... you have extended the incorrect class in a way.

By in a way, i mean that your table definitions are correct, but you are trying to use the table instances like rows. You can either add/change class usage as suggested above or you can pass the wor instance of user to the table class as an arg ument to refresh:

public function refresh(Zend_Db_Table_Row $user)
{
   $items = $user->findDependentRowset('UserItem', 'items');
   // do stuff with each item
   print_r($items);
   die();
}