-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow passing query Expression as column in Many-to-Many relationships (
- Loading branch information
1 parent
978ee88
commit fb8d09b
Showing
7 changed files
with
221 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
tests/Database/DatabaseEloquentBelongsToManyExpressionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Database; | ||
|
||
use Illuminate\Database\Capsule\Manager as DB; | ||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Database\Eloquent\Relations\MorphToMany; | ||
use Illuminate\Database\Query\Expression; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DatabaseEloquentBelongsToManyExpressionTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$db = new DB; | ||
|
||
$db->addConnection([ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
]); | ||
|
||
$db->bootEloquent(); | ||
$db->setAsGlobal(); | ||
|
||
$this->createSchema(); | ||
} | ||
|
||
public function testAmbiguousColumnsExpression(): void | ||
{ | ||
$this->seedData(); | ||
|
||
$tags = DatabaseEloquentBelongsToManyExpressionTestTestPost::findOrFail(1) | ||
->tags() | ||
->wherePivotNotIn(new Expression("tag_id || '_' || type"), ['1_t1']) | ||
->get(); | ||
|
||
$this->assertCount(1, $tags); | ||
$this->assertEquals(2, $tags->first()->getKey()); | ||
} | ||
|
||
public function testQualifiedColumnExpression(): void | ||
{ | ||
$this->seedData(); | ||
|
||
$tags = DatabaseEloquentBelongsToManyExpressionTestTestPost::findOrFail(2) | ||
->tags() | ||
->wherePivotNotIn(new Expression("taggables.tag_id || '_' || taggables.type"), ['2_t2']) | ||
->get(); | ||
|
||
$this->assertCount(1, $tags); | ||
$this->assertEquals(3, $tags->first()->getKey()); | ||
} | ||
|
||
/** | ||
* Setup the database schema. | ||
* | ||
* @return void | ||
*/ | ||
public function createSchema() | ||
{ | ||
$this->schema()->create('posts', fn (Blueprint $t) => $t->id()); | ||
$this->schema()->create('tags', fn (Blueprint $t) => $t->id()); | ||
$this->schema()->create('taggables', function (Blueprint $t) { | ||
$t->unsignedBigInteger('tag_id'); | ||
$t->unsignedBigInteger('taggable_id'); | ||
$t->string('type', 10); | ||
$t->string('taggable_type'); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Tear down the database schema. | ||
* | ||
* @return void | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
$this->schema()->drop('posts'); | ||
$this->schema()->drop('tags'); | ||
$this->schema()->drop('taggables'); | ||
} | ||
|
||
/** | ||
* Helpers... | ||
*/ | ||
protected function seedData(): void | ||
{ | ||
$p1 = DatabaseEloquentBelongsToManyExpressionTestTestPost::query()->create(); | ||
$p2 = DatabaseEloquentBelongsToManyExpressionTestTestPost::query()->create(); | ||
$t1 = DatabaseEloquentBelongsToManyExpressionTestTestTag::query()->create(); | ||
$t2 = DatabaseEloquentBelongsToManyExpressionTestTestTag::query()->create(); | ||
$t3 = DatabaseEloquentBelongsToManyExpressionTestTestTag::query()->create(); | ||
|
||
$p1->tags()->sync([ | ||
$t1->getKey() => ['type' => 't1'], | ||
$t2->getKey() => ['type' => 't2'], | ||
]); | ||
$p2->tags()->sync([ | ||
$t2->getKey() => ['type' => 't2'], | ||
$t3->getKey() => ['type' => 't3'], | ||
]); | ||
} | ||
|
||
/** | ||
* Get a database connection instance. | ||
* | ||
* @return \Illuminate\Database\ConnectionInterface | ||
*/ | ||
protected function connection() | ||
{ | ||
return Eloquent::getConnectionResolver()->connection(); | ||
} | ||
|
||
/** | ||
* Get a schema builder instance. | ||
* | ||
* @return \Illuminate\Database\Schema\Builder | ||
*/ | ||
protected function schema() | ||
{ | ||
return $this->connection()->getSchemaBuilder(); | ||
} | ||
} | ||
|
||
class DatabaseEloquentBelongsToManyExpressionTestTestPost extends Eloquent | ||
{ | ||
protected $table = 'posts'; | ||
protected $fillable = ['id']; | ||
public $timestamps = false; | ||
|
||
public function tags(): MorphToMany | ||
{ | ||
return $this->morphToMany( | ||
DatabaseEloquentBelongsToManyExpressionTestTestTag::class, | ||
'taggable', | ||
'taggables', | ||
'taggable_id', | ||
'tag_id', | ||
'id', | ||
'id', | ||
); | ||
} | ||
} | ||
|
||
class DatabaseEloquentBelongsToManyExpressionTestTestTag extends Eloquent | ||
{ | ||
protected $table = 'tags'; | ||
protected $fillable = ['id']; | ||
public $timestamps = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.