Skip to content

Commit 41b1c06

Browse files
committed
Merge branch 'fzhan-master'
2 parents 3e8a7ca + 4e97293 commit 41b1c06

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

core/MY_Model.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ public function with($relationship)
462462

463463
public function relate($row)
464464
{
465+
if (empty($row))
466+
{
467+
return $row;
468+
}
469+
465470
foreach ($this->belongs_to as $key => $value)
466471
{
467472
if (is_string($value))
@@ -477,14 +482,15 @@ public function relate($row)
477482

478483
if (in_array($relationship, $this->_with))
479484
{
480-
$this->load->model($options['model']);
485+
$this->load->model($options['model'], $relationship . '_model');
486+
481487
if (is_object($row))
482488
{
483-
$row->{$relationship} = $this->{$options['model']}->get($row->{$options['primary_key']});
489+
$row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']});
484490
}
485491
else
486492
{
487-
$row[$relationship] = $this->{$options['model']}->get($row[$options['primary_key']]);
493+
$row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]);
488494
}
489495
}
490496
}
@@ -504,14 +510,15 @@ public function relate($row)
504510

505511
if (in_array($relationship, $this->_with))
506512
{
507-
$this->load->model($options['model']);
513+
$this->load->model($options['model'], $relationship . '_model');
514+
508515
if (is_object($row))
509516
{
510-
$row->{$relationship} = $this->{$options['model']}->get_many_by($options['primary_key'], $row->{$this->primary_key});
517+
$row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key});
511518
}
512519
else
513520
{
514-
$row[$relationship] = $this->{$options['model']}->get_many_by($options['primary_key'], $row[$this->primary_key]);
521+
$row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]);
515522
}
516523
}
517524
}

tests/MY_Model_test.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public function test_belongs_to()
449449
$this->model->_database->expects($this->once())->method('row')->will($this->returnValue($object));
450450
$author_model->_database->expects($this->once())->method('row')->will($this->returnValue($author_object));
451451

452-
$this->model->load->expects($this->once())->method('model')->with('author_model')
452+
$this->model->load->expects($this->once())->method('model')->with('author_model', 'author_model')
453453
->will($this->returnCallback(function() use ($self, $author_model){
454454
$self->model->author_model = $author_model;
455455
}));
@@ -485,9 +485,9 @@ public function test_has_many()
485485
$this->model->_database->expects($this->once())->method('row')->will($this->returnValue($object));
486486
$comment_model->_database->expects($this->once())->method('result')->will($this->returnValue(array( $comment_object, $comment_object_2 )));
487487

488-
$this->model->load->expects($this->once())->method('model')->with('comment_model')
488+
$this->model->load->expects($this->once())->method('model')->with('comment_model', 'comments_model')
489489
->will($this->returnCallback(function() use ($self, $comment_model){
490-
$self->model->comment_model = $comment_model;
490+
$self->model->comments_model = $comment_model;
491491
}));
492492

493493
$this->assertEquals($expected_object, $this->model->with('comments')->get(1));

0 commit comments

Comments
 (0)