Skip to content

Commit 51c6801

Browse files
authored
some modifications for relations in subdirectories
as per issue avenirer#206. I sure hope it won't break anything else...
1 parent d1f8ebb commit 51c6801

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

core/MY_Model.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -1351,10 +1351,10 @@ private function _set_relationships()
13511351
$foreign_model = $relation;
13521352
$model = $this->_parse_model_dir($foreign_model);
13531353
$foreign_model = $model['foreign_model'];
1354-
$model_dir = $model['model_dir'];
1354+
//$model_dir = $model['model_dir'];
13551355
$foreign_model_name = $model['foreign_model_name'];
13561356

1357-
$this->load->model($model_dir . $foreign_model_name);
1357+
$this->load->model($foreign_model, $foreign_model_name);
13581358
$foreign_table = $this->{$foreign_model_name}->table;
13591359
$foreign_key = $this->{$foreign_model_name}->primary_key;
13601360
$local_key = $this->primary_key;
@@ -1368,20 +1368,20 @@ private function _set_relationships()
13681368
if($this->is_assoc($relation))
13691369
{
13701370
$foreign_model = $relation['foreign_model'];
1371+
$model = $this->_parse_model_dir($foreign_model);
1372+
$foreign_model = $model['foreign_model'];
1373+
$model_dir = $model['model_dir'];
1374+
$foreign_model_name = $model['foreign_model_name'];
1375+
$this->load->model($model_dir . $foreign_model, $foreign_model_name);
13711376
if(array_key_exists('foreign_table',$relation))
13721377
{
13731378
$foreign_table = $relation['foreign_table'];
13741379
}
13751380
else
13761381
{
1377-
$model = $this->_parse_model_dir($foreign_model);
1378-
$foreign_model = $model['foreign_model'];
1379-
$model_dir = $model['model_dir'];
1380-
$foreign_model_name = $model['foreign_model_name'];
1381-
1382-
$this->load->model($model_dir . $foreign_model_name);
13831382
$foreign_table = $this->{$foreign_model_name}->table;
13841383
}
1384+
13851385
$foreign_key = $relation['foreign_key'];
13861386
$local_key = $relation['local_key'];
13871387
if($option=='has_many_pivot')
@@ -1421,7 +1421,7 @@ private function _set_relationships()
14211421
$pivot_table = $tables[0].'_'.$tables[1];
14221422
}
14231423

1424-
$this->_relationships[$key] = array('relation' => $option, 'relation_key' => $key, 'foreign_model' => strtolower($foreign_model), 'foreign_table' => $foreign_table, 'foreign_key' => $foreign_key, 'local_key' => $local_key);
1424+
$this->_relationships[$key] = array('relation' => $option, 'relation_key' => $key, 'foreign_model' => strtolower($foreign_model), 'foreign_model_name'=>strtolower($foreign_model_name), 'foreign_table' => $foreign_table, 'foreign_key' => $foreign_key, 'local_key' => $local_key);
14251425
if($option == 'has_many_pivot')
14261426
{
14271427
$this->_relationships[$key]['pivot_table'] = $pivot_table;
@@ -1966,7 +1966,7 @@ protected function is_assoc(array $array) {
19661966
private function _parse_model_dir($foreign_model)
19671967
{
19681968
$data['foreign_model'] = $foreign_model;
1969-
$data['model_dir'] = NULL;
1969+
$data['model_dir'] = '';
19701970

19711971
$full_model = explode('/', $data['foreign_model']);
19721972
if ($full_model) {
@@ -1975,7 +1975,9 @@ private function _parse_model_dir($foreign_model)
19751975
$data['model_dir'] = str_replace($data['foreign_model'], null, implode('/', $full_model));
19761976
}
19771977

1978-
$data['foreign_model_name'] = strtolower($data['foreign_model']);
1978+
$foreign_model_name = str_replace('/','_',$data['model_dir'].$data['foreign_model']);
1979+
1980+
$data['foreign_model_name'] = strtolower($foreign_model_name);
19791981

19801982
return $data;
19811983
}

0 commit comments

Comments
 (0)