开发者

How to fetch data from another table within a Zend_Db model class?

I have two tables registries and names. Cons开发者_StackOverflow中文版equently, 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?


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
  }
}


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜