How can I get data from another table in a Zend_Db model class?

I have two tables registries

and names

. Hence, I have two model classes. I am coding a method in a registries model and I need to get all names in a table / name model. How did I do it?

Should a simple one new Names()

work? But is it recommended?

+2


a source to share


2 answers


Yes, it is possible to instantiate another table class inside the methods of another table class.

But if you have an application work that includes multiple tables, it is better to create a class that encapsulates that application and use both tables from it.

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

does it call the table as a model.

A model is a part of your application and may require multiple tables to do its job. The class corresponding to your table is a table.

Divide your models from your tables!




Repeat your comment: you still think the model is expanding Zend_Db_Table_Abstract

- it is not!

Model is not a data access class, it is a unit of your application. It does not cover 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
  }
}

      

+6


a source


To do this, you need to set up a table with a dependent relationship



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

      

+1


a source







All Articles