Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix corrupted data in 'allowedIncludes' #118

Merged
merged 4 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ protected function addIncludesToQuery(Collection $includes)
}

return [$fullRelationName => function ($query) use ($fields) {
$query->select($fields);
$query->select($this->prependFieldsWithTableName($fields, $query->getModel()->getTable()));
}];
});
})
Expand Down
8 changes: 4 additions & 4 deletions tests/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,27 @@ public function it_can_fetch_only_requested_columns_from_an_included_model()
$queryBuilder->first()->relatedModels;

$this->assertQueryLogContains('select "test_models"."id" from "test_models"');
$this->assertQueryLogContains('select "name" from "related_models"');
$this->assertQueryLogContains('select "related_models"."name" from "related_models"');
}

/** @test */
public function it_can_allow_specific_fields_on_an_included_model()
{
$request = new Request([
'fields' => ['related_models' => 'id,first_name'],
'fields' => ['related_models' => 'id,name'],
'include' => ['related-models'],
]);

$queryBuilder = QueryBuilder::for(TestModel::class, $request)
->allowedIncludes('related-models')
->allowedFields(['related_models.id', 'related_models.first_name']);
->allowedFields(['related_models.id', 'related_models.name']);

DB::enableQueryLog();

$queryBuilder->first()->relatedModels;

$this->assertQueryLogContains('select "test_models".* from "test_models"');
$this->assertQueryLogContains('select "id", "first_name" from "related_models"');
$this->assertQueryLogContains('select "related_models"."id", "related_models"."name" from "related_models"');
}

protected function createQueryFromFieldRequest(array $fields): QueryBuilder
Expand Down
18 changes: 18 additions & 0 deletions tests/IncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function setUp()
$model
->relatedModels()->create(['name' => 'Test'])
->nestedRelatedModels()->create(['name' => 'Test']);

$model->relatedThroughPivotModels()->create([
'id' => $model->id + 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

'name' => 'Test',
]);
});
}

Expand Down Expand Up @@ -140,6 +145,19 @@ public function it_can_include_multiple_model_relations()
$this->assertRelationLoaded($models, 'otherRelatedModels');
}

/** @test */
public function it_returns_correct_id_when_including_many_to_many_relationship()
{
$models = $this
->createQueryFromIncludeRequest('related-through-pivot-models')
->allowedIncludes('related-through-pivot-models')
->get();

$relatedModel = $models->first()->relatedThroughPivotModels->first();

$this->assertEquals($relatedModel->id, $relatedModel->pivot->related_through_pivot_model_id);
}

/** @test */
public function an_invalid_include_query_exception_contains_the_unknown_and_allowed_includes()
{
Expand Down
18 changes: 18 additions & 0 deletions tests/Models/RelatedThroughPivotModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Spatie\QueryBuilder\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class RelatedThroughPivotModel extends Model
{
protected $guarded = [];

public $timestamps = false;

public function testModels(): BelongsToMany
{
return $this->belongsToMany(TestModel::class, 'pivot_models');
}
}
6 changes: 6 additions & 0 deletions tests/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class TestModel extends Model
{
Expand All @@ -21,6 +22,11 @@ public function otherRelatedModels(): HasMany
return $this->hasMany(RelatedModel::class);
}

public function relatedThroughPivotModels(): BelongsToMany
{
return $this->belongsToMany(RelatedThroughPivotModel::class, 'pivot_models');
}

public function scopeNamed(Builder $query, string $name) : Builder
{
return $query->where('name', $name);
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ protected function setUpDatabase(Application $app)
$table->integer('related_model_id');
$table->string('name');
});

$app['db']->connection()->getSchemaBuilder()->create('pivot_models', function (Blueprint $table) {
$table->increments('id');
$table->string('test_model_id');
$table->integer('related_through_pivot_model_id');
});

$app['db']->connection()->getSchemaBuilder()->create('related_through_pivot_models', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
}

protected function getPackageProviders($app)
Expand Down