views:

160

answers:

2

I have two tables registries and names. Consequently, I have two model classes. I'm coding a method in registries model and I need to fetch all names in the names table/model. How'd I do it?

Should a simple new Names() work? But, is it recommended?

+3  A: 

Yes, it's possible to create an instance of another table class inside methods of another table class.

But if you have application work to do that involves multiple tables, it's better to design a class that encapsulates that application work, and use both tables from within it.

This is why it's inaccurate to call a table a model. Nowhere in the Zend_Db_Table documentation does it call a table a model.

A model represents some part of your application, and it may involve multiple tables in order to do its work. A class that corresponds to your table is just a Table.

Decouple your models from your tables!


Re your comment: You're still thinking that a model extends Zend_Db_Table_Abstract -- it doesn't!

A model is not a data access class, it's a unit of your application. It doesn't extend any part of the Zend Framework.

The relationship between model and table is HAS-A, not IS-A.

class MyRegistryModel // extends nothing
{
  /**
   * @var Zend_Db_Table_Abstract
   */
  protected $_registryTable;

  /**
   * @var Zend_Db_Table_Abstract
   */
  protected $_namesTable;

  public function __construct()
  {
    $this->_registryTable = new RegistryTable();
    $this->_namesTable = new NamesTable();
  }

  public function getDailyReport()
  {
    // use the tables as needed to build the report
  }
}
Bill Karwin
how do I work with more than 1 table, then? i can't set $_name = 'table1' and $_name = 'table2'. How would it work?
Rodrigo Alves
well explained! thanks!
Rodrigo Alves
+1  A: 

For this to work you have to have your table setup with a dependent relationship

registryTable->find(1)->current()->getDependentRowset($namesTable)
Ballsacian1
yep! that's what i'm using right now. Thx!
Rodrigo Alves