我正在研究在Zend Framework中创建一个与数据访问层分开的域层.数据访问层由两个主要对象组成,即表数据网关和行数据网关.根据Bill Karwin对此前一个问题的回复,我现在为我的域Person对象提供了以下代码:
class Model_Row_Person
{
protected $_gateway;
public function __construct(Zend_Db_Table_Row $gateway)
{
$this->_gateway = $gateway;
}
public function login($userName, $password)
{
}
public function setPassword($password)
{
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这仅适用于单个行.我还需要创建一个可以表示整个表的域对象,并且(可能)可以用来遍历表中的所有Person并返回适当类型的人(管理员,买家等)对象以供使用.基本上,我想象如下:
class Model_Table_Person implements SeekableIterator, Countable, ArrayAccess
{
protected $_gateway;
public function __construct(Model_DbTable_Person $gateway)
{
$this->_gateway = $gateway;
}
public function current()
{
$current = $this->_gateway->fetchRow($this->_pointer);
return $this->_getUser($current);
}
private function _getUser(Zend_Db_Table_Row $current)
{
switch($current->userType)
{
case 'admin':
return new Model_Row_Administrator($current);
break;
case 'associate':
return new Model_Row_Associate($current);
break;
} …Run Code Online (Sandbox Code Playgroud)