Skip to content

Commit 76756c2

Browse files
author
Marcel Gwerder
committed
Always use the fully qualified name for the keys, solves problems with queries that contain joins
1 parent 5ca984e commit 76756c2

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/Parser.php

100755100644
Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use \Illuminate\Database\Eloquent\Relations\HasMany;
66
use \Illuminate\Database\Eloquent\Relations\BelongsTo;
77
use \Illuminate\Database\Eloquent\Relations\BelongsToMany;
8+
use \Illuminate\Database\Eloquent\Relations\MorphOne;
9+
use \Illuminate\Database\Eloquent\Relations\MorphMany;
810
use \Illuminate\Database\Eloquent\Builder as EloquentBuilder;
911
use \Illuminate\Database\Query\Builder as QueryBuilder;
1012
use \Illuminate\Support\Facades\Config;
@@ -406,36 +408,36 @@ protected function parseWith($withParam)
406408
}
407409

408410
$relation = call_user_func([$previousModel, $part]);
409-
$model = $relation->getModel();
410411
$relationType = $this->getRelationType($relation);
411412

412-
if($relationType === 'HasManyThrough')
413+
if($relationType === 'BelongsTo')
413414
{
414-
$firstKey = $model->getKeyName();
415-
$secondKey = null;
416-
}
417-
else
418-
{
419-
$primaryKey = $model->getKeyName();
420-
$foreignKey = $relation->getForeignKey();
421-
}
422-
423-
//Switch keys according to the type of relationship
424-
if($relationType === 'HasOne' || $relationType === 'HasMany' || $relationType === 'BelongsToMany')
415+
$firstKey = $relation->getQualifiedForeignKey();
416+
$secondKey = $relation->getQualifiedParentKeyName();
417+
}
418+
else if($relationType === 'HasMany' || $relationType === 'HasOne')
425419
{
426-
$firstKey = $primaryKey;
427-
$secondKey = $foreignKey;
428-
}
429-
else if($relationType === 'BelongsTo')
420+
$firstKey = $relation->getQualifiedParentKeyName();
421+
$secondKey = $relation->getForeignKey();
422+
}
423+
else if($relationType === 'BelongsToMany')
430424
{
431-
$firstKey = $foreignKey;
432-
$secondKey = $primaryKey;
425+
$firstKey = $relation->getQualifiedParentKeyName();
426+
$secondKey = $relation->getRelated()->getQualifiedKeyName();
433427
}
428+
else if($relationType === 'HasManyThrough')
429+
{
430+
$firstKey = $relation->getHasCompareKey();
431+
$secondKey = null;
432+
}
433+
else {
434+
die('Relation type not supported!');
435+
}
434436

435437
//Check if we're on level 1 (e.g. a and not a.b)
436438
if($firstKey !== null && $previousHistoryPath == '')
437439
{
438-
if($fieldsCount > 0 && !in_array($primaryKey, $fields))
440+
if($fieldsCount > 0 && !in_array($firstKey, $fields))
439441
{
440442
$fields[] = $firstKey;
441443
}
@@ -453,7 +455,7 @@ protected function parseWith($withParam)
453455
$withHistory[$currentHistoryPath]['fields'][] = $secondKey;
454456
}
455457

456-
$previousModel = $model;
458+
$previousModel = $relation->getModel();
457459
}
458460

459461
unset($previousModel);
@@ -788,6 +790,16 @@ protected function getRelationType($relation)
788790
{
789791
return 'HasManyThrough';
790792
}
793+
794+
if($relation instanceof MorphOne)
795+
{
796+
return 'MorphOne';
797+
}
798+
799+
if($relation instanceof MorphMany)
800+
{
801+
return 'MorphMany';
802+
}
791803
}
792804

793805
/**

tests/ApiHandlerTest.php

100755100644
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function setUp()
9595
$processor = new Illuminate\Database\Query\Processors\MySqlProcessor;
9696
$connection = m::mock('Illuminate\Database\ConnectionInterface', ['getQueryGrammar' => $grammar, 'getPostProcessor' => $processor]);
9797
$connection->shouldReceive('select')->once()->with('select * from `posts`', [])->andReturn($this->data);
98+
$connection->shouldReceive('select')->once()->with('select * from `posts`', [], true)->andReturn($this->data);
9899
$connection->shouldReceive('raw')->once()->with('MATCH(title,description) AGAINST("Something to search" IN BOOLEAN MODE) as `_score`')
99100
->andReturn($this->fulltextSelectExpression);
100101
$connection->shouldReceive('getPdo')->once()->andReturn($pdo);
@@ -224,7 +225,7 @@ public function testGetBuilder()
224225
$this->assertArrayHasKey('comments.user', $eagerLoads);
225226

226227
//Check if auto fields are set on the base query
227-
$this->assertContains('id', $columns);
228+
$this->assertContains('posts.id', $columns);
228229

229230
//Check if fields are set on the "comments" relation query
230231
$query = $post->newQuery();

0 commit comments

Comments
 (0)