From d6df3fe174a78194a0184edd5146d376d579bd4c Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 23 Apr 2019 16:37:19 +0300 Subject: [PATCH] fix with in queryset --- src/Phact/Orm/QueryLayer.php | 3 ++- tests/Cases/Orm/Abs/AbstractQuerySetTest.php | 23 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Phact/Orm/QueryLayer.php b/src/Phact/Orm/QueryLayer.php index 3e34165..d3253a7 100644 --- a/src/Phact/Orm/QueryLayer.php +++ b/src/Phact/Orm/QueryLayer.php @@ -420,8 +420,9 @@ public function defaultSelect() $table = $this->getRelationTable($relationName); $relationModel = $this->getRelationModel($relationName); $attributes = $relationModel->getFieldsManager()->getDbAttributesList(); + $alias = $this->getAlias($relationName, $table); foreach ($attributes as $attribute) { - $select[$this->column($table, $attribute)] = $relationName . '__' . $attribute; + $select[$this->column($alias, $attribute)] = $this->quote($relationName . '__' . $attribute); } } return $select; diff --git a/tests/Cases/Orm/Abs/AbstractQuerySetTest.php b/tests/Cases/Orm/Abs/AbstractQuerySetTest.php index 1f55862..4dad120 100644 --- a/tests/Cases/Orm/Abs/AbstractQuerySetTest.php +++ b/tests/Cases/Orm/Abs/AbstractQuerySetTest.php @@ -442,4 +442,27 @@ public function testDynamicProperties() $this->assertEquals("SELECT {$q}test_note{$q}.* FROM {$q}test_note{$q} LEFT JOIN {$q}test_note_property_char_value{$q} {$q}test_note_property_char_value_1{$q} ON {$q}test_note{$q}.{$q}id{$q} = {$q}test_note_property_char_value_1{$q}.{$q}note_id{$q} LEFT JOIN {$q}test_note_property_int_value{$q} {$q}test_note_property_int_value_2{$q} ON {$q}test_note{$q}.{$q}id{$q} = {$q}test_note_property_int_value_2{$q}.{$q}note_id{$q} LEFT JOIN {$q}test_note_property_int_value{$q} {$q}test_note_property_int_value_3{$q} ON {$q}test_note{$q}.{$q}id{$q} = {$q}test_note_property_int_value_3{$q}.{$q}note_id{$q} WHERE (({$q}test_note_property_char_value_1{$q}.{$q}note_property_id{$q} = 1) AND ({$q}test_note_property_char_value_1{$q}.{$q}value{$q} = 'Some description for first note')) AND (({$q}test_note_property_int_value_2{$q}.{$q}note_property_id{$q} = 2) AND ({$q}test_note_property_int_value_2{$q}.{$q}value{$q} = 3)) AND (({$q}test_note_property_int_value_3{$q}.{$q}note_property_id{$q} = 10) AND ({$q}test_note_property_int_value_3{$q}.{$q}value{$q} = 10))", $sql); $this->assertEquals(0, count($qs->all())); } + + public function testWith() + { + $q = $this->getQuoteCharacter(); + + $note = new Note(); + $note->name = 'new note'; + $note->save(); + + $thesis = new NoteThesis(); + $thesis->name = 'new thesis'; + $thesis->note = $note; + $thesis->save(); + + $qs = NoteThesis::objects()->with(['note']); + $sql = $qs->getSql(); + + $this->assertEquals("SELECT {$q}test_note_thesis{$q}.*, {$q}test_note_1{$q}.{$q}name{$q} AS {$q}note__name{$q}, {$q}test_note_1{$q}.{$q}id{$q} AS {$q}note__id{$q} FROM {$q}test_note_thesis{$q} LEFT JOIN {$q}test_note{$q} {$q}test_note_1{$q} ON {$q}test_note_thesis{$q}.{$q}note_id{$q} = {$q}test_note_1{$q}.{$q}id{$q}", $sql); + + $all = $qs->all(); + + $this->assertEquals(1, count($all)); + } } \ No newline at end of file