Skip to content

Commit

Permalink
Merge pull request #11 from s0de/feature
Browse files Browse the repository at this point in the history
fix with in queryset and written test for this
  • Loading branch information
AntonOkulov authored Apr 23, 2019
2 parents 8c499e6 + d6df3fe commit 15a5459
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Phact/Orm/QueryLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions tests/Cases/Orm/Abs/AbstractQuerySetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 15a5459

Please sign in to comment.