如何从Zend_Db模型类中的另一个表中获取数据?

Rod*_*uza 2 php zend-framework model

我有两个表registriesnames.因此,我有两个模型类.我正在编写注册表模型中的方法,我需要获取名称表/模型中的所有名称.我怎么做的?

应该简单的new Names()工作吗?但是,推荐吗?

Bil*_*win 6

是的,可以在另一个表类的方法中创建另一个表类的实例.

但是如果您要执行涉及多个表的应用程序工作,那么最好设计一个封装该应用程序工作的类,并使用其中的两个表.

这就是为什么将表称为模型是不准确的.无处在Zend_Db_Table文档中并调用它的表的模型.

模型代表应用程序的某些部分,它可能涉及多个表以完成其工作.与您的表对应的类只是一个表.

将模型与桌子分离!


你的评论:你仍然认为模型扩展Zend_Db_Table_Abstract- 它没有!

模型不是数据访问类,它是应用程序的一个单元.它没有扩展Zend Framework的任何部分.

模型和表之间的关系是HAS-A,而不是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
  }
}
Run Code Online (Sandbox Code Playgroud)