1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Spot
  • 7,962
  • 9
  • 46
  • 55
  • It might be clearer if you described the desired behaviour from the outside - what do you want a user of your class to be able to achieve? Then you can worry about the implementation... – Paul Dixon Aug 28 '09 at 07:49

2 Answers2

1

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.

Community
  • 1
  • 1
soulmerge
  • 73,842
  • 19
  • 118
  • 155
0

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
  • 897
  • 1
  • 7
  • 17