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?

2 Answers 2

6

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
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

how do I work with more than 1 table, then? i can't set $_name = 'table1' and $_name = 'table2'. How would it work?
1

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

registryTable->find(1)->current()->getDependentRowset($namesTable)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.