Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for expanding beyond pluralized table names, as well as adding field data support #20

Closed
ariven opened this issue Apr 29, 2012 · 3 comments

Comments

@ariven
Copy link

ariven commented Apr 29, 2012

Not all of my table names are pluralized, for example, sometimes it is agent instead of agents and other variations on that theme.

In those environments, it helps to have a fallback to singular format.

Also, I like to have access to the fields and their type as returned by CodeIgniter. In addition grabbing the field data from
codeigniter gives us a chance to properly guess the primary key field, in case it isn't 'id'

This is what I ended up with:

/**
 * First pulls _m and _model off the name of the class, then pluralizes the result.  If the table
 * doesn't exist, tries the singular version (i.e. if it was named agent_model for table agent,
 * instead of table agents).
 *
 * @return void
 */
private function _fetch_table()
{
    if (($this->_table == NULL) or ($this->_table == 'MY_Model'))
    {
        $class     = preg_replace('/(_m|_model)?$/', '', get_class($this));
        $plural_form = plural(strtolower($class));
        if ($this->db->table_exists($plural_form))
        {
            // pluralized form exists
            $this->_table = $plural_form;
        }
        else
        {
            // fall back to non plural form
            $this->_table = $strtolower($class);
        }
        // load the fields into the $fields array using information from CodeIgniter
        $this->_set_fields();
    }
}

/**
 * Setting the fields pulls the master field information from CI and sets an array of name=>type
 * in case you want to use the type for validation, etc.
 * 
 * It also tries to guess the primary_key by using the primary_key flag in the field_data.
 * @return void
 */
private function _set_fields()
{
    if ($this->db->table_exists($this->_table))
    {
        $table_fields = $this->db->field_data($this->_table);
        foreach ($table_fields as $field)
        {
            if ($field->primary_key == '1')
            {
                $this->primary_key = $field->name;
            }
            $this->fields[$field->name] = $field->type;
        }
    }
}
@jamierumbelow
Copy link
Owner

If you need to customise the table or primary key you can do so by setting the class variable in your model:

class Agent_model extends MY_Model
{
    protected $_table = 'agent';
    protected $primary_key = 'agent_id';
}

As for the _set_fields() idea, I'm not really sure what you'd gain by having that data. Certainly, it wouldn't be necessary to be run on every request (and thus adding another two queries to each page load).

@ariven
Copy link
Author

ariven commented Apr 29, 2012

You are right the set_fields() is an extra gimme that I came up with when working on form builders and such, its useful when you want a full list of everything to iterate over or build things dependent on, but isn't really needed for every use of a model.

The reason I went with the change to _set_fields() was to avoid having to override stuff every time I added a model... it just automagically grabs the name for me and saves me typing and remembering to edit stuff when things change. With that change I just have to include the class line and closure.

@jamierumbelow
Copy link
Owner

I really appreciate the suggestions, but I don't feel like they fit into the remit of MY_Model. It's built to be lightweight, small and fast, and any extra queries (particularly adding them per-request) must be incredibly well justified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants