You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
}
}
}
The text was updated successfully, but these errors were encountered:
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).
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.
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.
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:
The text was updated successfully, but these errors were encountered: