views:

68

answers:

2

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 fucntion load($_entryId)
    {
        // Code to load an entry into $this->_entry;
    }

    public fucntion 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 implmentation and make it work for multiple records, in a clean manner.

Ideas?

+1  A: 

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.

soulmerge
I really do like my wheel tho :P
Spot
Ok, you're right.
Spot
A: 

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.

Tom Schaefer