开发者

select queries with Zend_DB_Table

I have a code something like following

class Application_Model_Company  extends Zend_Db_Table_Abstract {
 protected $_name = 'companies';
 private $id;
 private $name;
 private $shortName;
 private $description;

 public st开发者_如何学JAVAatic function getAllCompanies() {
 $companyObj = new self();
 $select  = $companyObj->select()->order(array('name'));
 $rows = $companyObj->fetchAll($select);
 if($rows) {
  $companies = array();
   foreach($rows as $row) {
    $company = new self();
    $company->id = $row->id;
    $company->name = $row->name;
    $company->shortName = $row->short_name;
    $company->description = $row->description;
    $companies[] = $comapny;
  }
    // return Company Objects
    return $companies;
  }else
   throw new Exception('Oops..');
 }
}

I need to return Company Objects from getAllCompanies() function, But it returns Zend_Db_Table_Row Object. How do I correct this?


Your Model class shouldnt extend the table class. The table class is separate. Your Model should extend the row class if extending anything from Zend_Db at all. Also you shouldnt put retrieval methods on your Model classes directly, they would go on the table classes.

This is because in the paradigm youre trying to use here, a Model represents a single Row of data, the Table class represents the table as a repository of data, and the Rowset class represents a collection of Rows (or Models).

To properly implement what you are describing in your question you would do something like the following:

class Application_Model_DbTable_Company extends Zend_Db_Table_Abstract
{
   // table name
   protected $_name = 'company';

   protected _$rowClass = 'Application_Model_Company';

   // your custom retrieval methods
}

class Application_Model_Company extends Zend_Db_Table_Row
{
  protected $_tableClass = 'Application_Model_DbTable_Company';
  // custom accessors and mutators
}

However, using some kind of implementation of the Data Mapper pattern is whats actually recommended. Check out the Quickstart for a thorough tutorial on a simplified implementation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜