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

Made model suffix '_model' configurable. #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions core/MY_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class MY_Model extends CI_Model
*/
protected $return_type = 'object';
protected $_temporary_return_type = NULL;

/**
* Model suffix for relationships
*/
protected $model_suffix = '_model';

/* --------------------------------------------------------------
* GENERIC METHODS
Expand Down Expand Up @@ -457,7 +462,7 @@ public function relate($row)
if (is_string($value))
{
$relationship = $value;
$options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' );
$options = array( 'primary_key' => $value . '_id', 'model' => $value . $this->model_suffix);
}
else
{
Expand All @@ -467,15 +472,15 @@ public function relate($row)

if (in_array($relationship, $this->_with))
{
$this->load->model($options['model'], $relationship . '_model');
$this->load->model($options['model'], $relationship . $this->model_suffix);

if (is_object($row))
{
$row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']});
$row->{$relationship} = $this->{$relationship . $this->model_suffix}->get($row->{$options['primary_key']});
}
else
{
$row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]);
$row[$relationship] = $this->{$relationship . $this->model_suffix}->get($row[$options['primary_key']]);
}
}
}
Expand All @@ -485,7 +490,7 @@ public function relate($row)
if (is_string($value))
{
$relationship = $value;
$options = array( 'primary_key' => singular($this->_table) . '_id', 'model' => singular($value) . '_model' );
$options = array( 'primary_key' => singular($this->_table) . '_id', 'model' => singular($value) . $this->model_suffix);
}
else
{
Expand All @@ -495,15 +500,15 @@ public function relate($row)

if (in_array($relationship, $this->_with))
{
$this->load->model($options['model'], $relationship . '_model');
$this->load->model($options['model'], $relationship . $this->model_suffix);

if (is_object($row))
{
$row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key});
$row->{$relationship} = $this->{$relationship . $this->model_suffix}->get_many_by($options['primary_key'], $row->{$this->primary_key});
}
else
{
$row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]);
$row[$relationship] = $this->{$relationship . $this->model_suffix}->get_many_by($options['primary_key'], $row[$this->primary_key]);
}
}
}
Expand Down Expand Up @@ -855,7 +860,7 @@ private function _fetch_table()
{
if ($this->_table == NULL)
{
$this->_table = plural(preg_replace('/(_m|_model)?$/', '', strtolower(get_class($this))));
$this->_table = plural(preg_replace('/(_m|' . $this->model_suffix . ')?$/', '', strtolower(get_class($this))));
}
}

Expand Down Expand Up @@ -890,4 +895,4 @@ protected function _return_type($multi = FALSE)
$method = ($multi) ? 'result' : 'row';
return $this->_temporary_return_type == 'array' ? $method . '_array' : $method;
}
}
}