Skip to content
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
5 changes: 3 additions & 2 deletions src/Relation/BulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Reference\ReferenceInterface;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Select\Options\LoadOptions;
use Cycle\ORM\Select\UpdateLoader;
use Cycle\ORM\Service\EntityFactoryInterface;
use Cycle\ORM\Service\SourceProviderInterface;
Expand All @@ -34,7 +35,7 @@ public function collect(object ...$entities): RelationLoaderInterface
{
if ($entities === []) {
return new class implements RelationLoaderInterface {
public function load(string $relation, array $options = []): static
public function load(string $relation, LoadOptions|array $options = []): static
{
return $this;
}
Expand Down Expand Up @@ -65,7 +66,7 @@ public function run(): void {}
return $clone;
}

public function load(string $relation, array $options = []): static
public function load(string $relation, LoadOptions|array $options = []): static
{
$this->loader->loadRelation($relation, $options, load: true);

Expand Down
7 changes: 4 additions & 3 deletions src/Relation/RelationLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Cycle\ORM\Relation;

use Cycle\ORM\Select;
use Cycle\ORM\Select\Options\LoadOptions;

/**
* Relations loader
Expand All @@ -23,11 +24,11 @@ interface RelationLoaderInterface
* Define relation to be loaded.
*
* @param non-empty-string $relation Relation name
* @param array $options Relation loading options
* @param LoadOptions|array $options Relation loading options
*
* @see Select::load() for available options\
* @see Select::load() for available options
*/
public function load(string $relation, array $options = []): static;
public function load(string $relation, LoadOptions|array $options = []): static;

/**
* Execute relation loading for all collected entities.
Expand Down
49 changes: 49 additions & 0 deletions tests/ORM/Unit/Relation/BulkLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Cycle\ORM\Relation\BulkLoader;
use Cycle\ORM\Relation\RelationLoaderInterface;
use Cycle\ORM\Schema;
use Cycle\ORM\Select\Options\HasOneLoadOptions;
use Cycle\ORM\Select\Options\LoadOptions;
use Cycle\ORM\Tests\Fixtures\OneWayUuidTypecast;
use Cycle\ORM\Tests\Fixtures\Profile;
use Cycle\ORM\Tests\Fixtures\User;
Expand Down Expand Up @@ -206,6 +208,53 @@ public function testLoadWithCustomOptions(): void
$this->assertSame($loader, $result);
}

/**
* Test load with LoadOptions DTO
*/
public function testLoadWithLoadOptionsDTO(): void
{
$orm = $this->createORM();

$entity = new User();
$loader = new BulkLoader($orm);
$loader = $loader->collect($entity);
$result = $loader->load('profile', new LoadOptions());

$this->assertSame($loader, $result);
}

/**
* Test load with HasOneLoadOptions DTO
*/
public function testLoadWithHasOneLoadOptionsDTO(): void
{
$orm = $this->createORM();

$entity = new User();
$loader = new BulkLoader($orm);
$loader = $loader->collect($entity);
$result = $loader->load('profile', new HasOneLoadOptions(
where: ['id' => 1],
));

$this->assertSame($loader, $result);
}

/**
* Test empty collect returns loader that accepts LoadOptions DTO
*/
public function testEmptyCollectLoadAcceptsLoadOptionsDTO(): void
{
$orm = $this->createORM();
$loader = (new BulkLoader($orm))->collect();

self::assertInstanceOf(RelationLoaderInterface::class, $loader);
$result = $loader->load('profile', new HasOneLoadOptions());
self::assertSame($loader, $result);

$loader->run();
}

/**
* BulkLoader should handle Stringable PK values when typecast is one-directional.
*
Expand Down
Loading