From b7f7ad884ba676689e71add77f86b9711f876dbd Mon Sep 17 00:00:00 2001 From: Jihun Lee Date: Thu, 19 May 2016 11:52:51 +0900 Subject: [PATCH] test eager loading belongs to nested with same foreign key --- test/RelationshipTest.php | 17 +++++++++++++++++ test/fixtures/books.csv | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/test/RelationshipTest.php b/test/RelationshipTest.php index d3ce103d6..f8caefd8b 100644 --- a/test/RelationshipTest.php +++ b/test/RelationshipTest.php @@ -658,6 +658,23 @@ public function test_eager_loading_belongs_to_nested() $this->assert_sql_has("WHERE author_id IN(?,?)",ActiveRecord\Table::load('AwesomePerson')->last_sql); } + public function test_eager_loading_belongs_to_nested_with_same_foreign_key() + { + Author::$has_many = array(array('awesome_people')); + + $books = Book::find(array(2,3), array('include' => array('author' => array('awesome_people')))); + + foreach ($books as $book) + { + $this->assert_equals($book->author_id,$book->author->author_id); + $this->assert_equals($book->author->author_id,$book->author->awesome_people[0]->author_id); + } + + $this->assert_sql_has("WHERE book_id IN(?,?)",ActiveRecord\Table::load('Book')->last_sql); + $this->assert_sql_has("WHERE author_id IN(?,?)",ActiveRecord\Table::load('Author')->last_sql); + $this->assert_sql_has("WHERE author_id IN(?,?)",ActiveRecord\Table::load('AwesomePerson')->last_sql); + } + public function test_eager_loading_belongs_to_with_no_related_rows() { $e1 = Event::create(array('venue_id' => 200, 'host_id' => 200, 'title' => 'blah','type' => 'Music')); diff --git a/test/fixtures/books.csv b/test/fixtures/books.csv index 525afb3cc..128439773 100644 --- a/test/fixtures/books.csv +++ b/test/fixtures/books.csv @@ -1,3 +1,4 @@ book_id,author_id,secondary_author_id,name,special 1,1,2,"Ancient Art of Main Tanking",0 -2,2,2,"Another Book",0 \ No newline at end of file +2,2,2,"Another Book",0 +3,2,2,"Another Book Extended",0 \ No newline at end of file