【问题标题】:PHP Data Access to Multiple RecordsPHP 数据访问多条记录
【发布时间】:2010-11-23 15:41:11
【问题描述】:

所以目前我正在使用一种模式来获取数据条目(记录)。如果我只需要处理一张唱片,它对我来说非常有用。但是,如果涉及多个记录,则变得更加复杂。

这是我的基本模式,使用联系人表:

class Contacts_Entry {

    private $_entry = Array('contact_id' => false,
                            'name'       => false,
                            'email'      => false,
                            'phone'      => false,
                            'type'       => false );

    private $_entryKey = Array('contact_id' => 'i',
                               'name'       => 's',
                               'email'      => 's',
                               'phone'      => 's',
                               'type'       => 'i' );


    public function __call($_method, $_arguments)
    {
        /* API: Get */
        if (substr($_method, 0, 3) == 'get') {
            return $this->_getElement(camelCaseToUnderscore(substr($_method, 3)));
        }
        /* API: Set */
        if (substr($_method, 0, 3) == 'set' && count($_arguments) == 1) {
            return $this->_setElement(camelCaseToUnderscore(substr($_method, 3)), $_arguments[0]);
        }
        unset($_method,$_arguments);
        return false;
    }

    private function _getElement($_element)
    {
        if (!array_key_exists($_element, $this->_entry)) { return false; }

        if ($this->_entryKey[$_element] == 's') {
            if (!strlen($this->_entry[$_element])) { return false; }
        } elseif ($this->_entryKey[$_element] == 'i') {
            if (!strlen($this->_entry[$_element]) || !is_numeric($this->_entry[$_element])) { return false; }
        } elseif ($this->_entryKey[$_element] == 'a') {
            if (!count($this->_entry[$_element])) { return false; }
        } else {
            return false;
        }

        return $this->_entry[$_element];
    }

    private function _setElement($_element, $_data)
    {
        if (!array_key_exists($_element, $this->_entry)) { return false; }

        if ($this->_entryKey[$_element] == 's') {
            if (!strlen($_data)) { return false; }
        } elseif ($this->_entryKey[$_element] == 'i') {
            if (!strlen($_data) || !is_numeric($_data)) { return false; }
        } elseif ($this->_entryKey[$_element] == 'a') {
            if (!count($_data)) { return false; }
        } else {
            return false;
        }

        if ($this->_entry[$_element] = $_data) { return true; }
        return false;
    }

    public function load($_entryId)
    {
        // Code to load an entry into $this->_entry;
    }

    public function save()
    {
        // Code to save an entry from $this->_entry;
    }
}

如您所见,这对于单个记录非常有效。我什至可以将此对象传递给 Smarty,并在模板中使用 getMethod()。

但我需要帮助思考的是,采用这种实现并使其以干净的方式适用于多条记录的好方法。

【问题讨论】:

  • 如果您从外部描述所需的行为可能会更清楚 - 您希望您的班级的用户能够实现什么?然后你可以担心实现......

标签: php design-patterns oop data-access


【解决方案1】:

您正在重新发明轮子。先看看existing ORM(或ORM article in the wikipedia),然后再决定是否需要自己实现。

【讨论】:

  • 我真的很喜欢我的轮子:P
【解决方案2】:

为什么不简单地将它用作对象列表(数组)。所以你可以遍历数组。每个数组节点都有自己的对象。就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多