From b9a52102ed96c6743d702c7db7939f1512a388e2 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Mon, 29 Jul 2024 19:27:46 -0600 Subject: [PATCH] fixed duplicated sql syntax if join called twice. --- src/ActiveRecord.php | 1 + tests/ActiveRecordPdoIntegrationTest.php | 26 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 4b6fbaf..e790340 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -317,6 +317,7 @@ protected function resetQueryData(): self { $this->params = []; $this->sqlExpressions = []; + $this->join = null; return $this; } /** diff --git a/tests/ActiveRecordPdoIntegrationTest.php b/tests/ActiveRecordPdoIntegrationTest.php index 8796778..e0d3816 100644 --- a/tests/ActiveRecordPdoIntegrationTest.php +++ b/tests/ActiveRecordPdoIntegrationTest.php @@ -183,6 +183,32 @@ public function testJoin() $this->assertEquals($user->address, $contact->address); } + public function testJoinIsClearedAfterCalledTwice() + { + $user = new User(new PDO('sqlite:test.db')); + $user->name = 'demo'; + $user->password = md5('demo'); + $user->insert(); + + $contact = new Contact(new PDO('sqlite:test.db')); + $contact->user_id = $user->id; + $contact->email = 'test@amail.com'; + $contact->address = 'test address'; + $contact->insert(); + + $user->select('*, c.email, c.address')->join('contact as c', 'c.user_id = user.id')->find(); + // email and address will stored in user data array. + $this->assertEquals($user->id, $contact->user_id); + $this->assertEquals($user->email, $contact->email); + $this->assertEquals($user->address, $contact->address); + + $user->select('*, c.email, c.address')->join('contact as c', 'c.user_id = user.id')->find(); + // email and address will stored in user data array. + $this->assertEquals($user->id, $contact->user_id); + $this->assertEquals($user->email, $contact->email); + $this->assertEquals($user->address, $contact->address); + } + public function testQuery() { $user = new class (new PDO('sqlite:test.db')) extends User {