PHP 数据访问多条记录

发布于 2024-08-02 17:31:25 字数 2825 浏览 5 评论 0原文

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

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

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()。

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

So currently I am using a pattern to grab a data entry (record). It works great for me if I only need to work with one record. However if more than one record is involved it gets more complicated.

Here is my base pattern, using a contacts table:

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

As you can see, this works very well for single records. I can even pass this object to Smarty, and use the getMethod()s inside a template.

But what I need help thinking up, is a good way to take this kind of implementation and make it work for multiple records, in a clean manner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千年*琉璃梦 2024-08-09 17:31:25

你正在重新发明轮子。查看现有的ORM(或者在维基百科中的 ORM 文章),然后再决定是否需要自己实现。

You're reinventing the wheel. Have a look at existing ORM's (or at the ORM article in the wikipedia) first, before deciding that you need to implement one yourself.

浅浅 2024-08-09 17:31:25

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

Why don't you simply use it as a list of objects (array). So you can iterate through the array. Each array node has its own object. That's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文