6

我正在努力在 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)
    {

    }
}

但是,这仅适用于单个行。我还需要创建一个可以表示整个表的域对象,并且(可能)可以用来遍历表中的所有人员并返回适当类型的人员(管理员、买家等)对象以供使用。基本上,我设想如下:

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

这是处理这个特定问题的好/坏方法吗?我应该对整体设计进行哪些改进或调整?

提前感谢您的评论和批评。

4

1 回答 1

9

我记得您将使用域模型类来完全隐藏您使用数据库表进行持久性的事实。所以传递一个 Table 对象或一个 Row 对象应该完全在幕后:

<?php
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

$db = Zend_Db::factory('mysqli', array('dbname'=>'test',
    'username'=>'root', 'password'=>'xxxx'));
Zend_Db_Table_Abstract::setDefaultAdapter($db);

class Table_Person extends Zend_Db_Table_Abstract
{
    protected $_name = 'person';
}

class Model_Person
{
    /** @var Zend_Db_Table */
    protected static $table = null;

    /** @var Zend_Db_Table_Row */
    protected $person;

    public static function init() {
        if (self::$table == null) {
            self::$table = new Table_Person();
        }
    }

    protected static function factory(Zend_Db_Table_Row $personRow) {
        $personClass = 'Model_Person_' . ucfirst($personRow->person_type);
        return new $personClass($personRow);
    }

    public static function get($id) {
        self::init();
        $personRow = self::$table->find($id)->current();
        return self::factory($personRow);
    }

    public static function getCollection() {
        self::init();
        $personRowset = self::$table->fetchAll();
        $personArray = array();
        foreach ($personRowset as $person) {
            $personArray[] = self::factory($person);
        }
        return $personArray;
    }

    // protected constructor can only be called from this class, e.g. factory()
    protected function __construct(Zend_Db_Table_Row $personRow) {
        $this->person = $personRow;
    }

    public function login($password) {
        if ($this->person->password_hash ==
            hash('sha256', $this->person->password_salt . $password)) {
            return true;
        } else {
            return false;
        }

    }

    public function setPassword($newPassword) {
        $this->person->password_hash = hash('sha256',
            $this->person->password_salt . $newPassword);
        $this->person->save();
    }
}

class Model_Person_Admin extends Model_Person { }
class Model_Person_Associate extends Model_Person { }

$person = Model_Person::get(1);
print "Got object of type ".get_class($person)."\n";
$person->setPassword('potrzebie');

$people = Model_Person::getCollection();
print "Got ".count($people)." people objects:\n";
foreach ($people as $i => $person) {
    print "\t$i: ".get_class($person)."\n";
}

“我认为静态方法不好,这就是为什么我试图将表级方法创建为实例方法。”

我不接受任何总是不好的笼统声明static,或者单身总是不好的,或者goto总是不好的,或者你有什么。做出如此明确声明的人正在寻求过度简化问题。适当地使用语言工具,它们会对你有好处。

也就是说,当您选择一种语言结构时,通常会进行权衡,它使某些事情变得更容易,而做其他事情则变得更难。人们经常指出static难以编写单元测试代码,而且 PHP 在静态和子类化方面存在一些令人讨厌的缺陷。但也有一些优势,正如我们在这段代码中看到的那样。您必须根据具体情况自行判断优点是否大于缺点。

“Zend 框架会支持 Finder 类吗?”

我不认为这是必要的。

“您将 find 方法重命名为模型类中的 get 是否有特殊原因?”

我命名该方法get()只是为了区别于find(). “getter”范式与 OO 接口相关联,而“finders”传统上与数据库相关联。我们正在尝试设计域模型以假装不涉及数据库。

“你会继续使用相同的逻辑来实现特定的 getBy 和 getCollectionBy 方法吗?”

我反对创建一个泛型getBy()方法,因为它很容易接受一个泛型 SQL 表达式,然后逐字传递给数据访问对象。这将我们的域模型的使用与底层数据库表示相结合。

于 2008-12-17T01:33:24.570 回答