Skip to content

Commit 18e0b84

Browse files
markinigorroxblnfk
andauthored
Add orderBy to HasOneLoader (#487)
--------- Co-authored-by: roxblnfk <roxblnfk@ya.ru>
1 parent f91272d commit 18e0b84

File tree

4 files changed

+495
-1
lines changed

4 files changed

+495
-1
lines changed

src/Select/Loader/HasOneLoader.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
namespace Cycle\ORM\Select\Loader;
66

7+
use Cycle\Database\Query\SelectQuery;
8+
use Cycle\ORM\FactoryInterface;
79
use Cycle\ORM\Parser\AbstractNode;
810
use Cycle\ORM\Parser\SingularNode;
11+
use Cycle\ORM\Service\SourceProviderInterface;
912
use Cycle\ORM\Relation;
1013
use Cycle\ORM\SchemaInterface;
1114
use Cycle\ORM\Select\JoinableLoader;
1215
use Cycle\ORM\Select\Traits\JoinOneTableTrait;
16+
use Cycle\ORM\Select\Traits\OrderByTrait;
1317
use Cycle\ORM\Select\Traits\WhereTrait;
14-
use Cycle\Database\Query\SelectQuery;
1518

1619
/**
1720
* Dedicated to load HAS_ONE relations, by default loader will prefer to join data into query.
@@ -25,6 +28,7 @@
2528
class HasOneLoader extends JoinableLoader
2629
{
2730
use JoinOneTableTrait;
31+
use OrderByTrait;
2832
use WhereTrait;
2933

3034
/**
@@ -38,8 +42,22 @@ class HasOneLoader extends JoinableLoader
3842
'as' => null,
3943
'using' => null,
4044
'where' => null,
45+
'orderBy' => null,
4146
];
4247

48+
public function __construct(
49+
SchemaInterface $ormSchema,
50+
SourceProviderInterface $sourceProvider,
51+
FactoryInterface $factory,
52+
string $name,
53+
string $target,
54+
array $schema,
55+
) {
56+
parent::__construct($ormSchema, $sourceProvider, $factory, $name, $target, $schema);
57+
$this->options['where'] = $schema[Relation::WHERE] ?? [];
58+
$this->options['orderBy'] = $schema[Relation::ORDER_BY] ?? [];
59+
}
60+
4361
public function configureQuery(SelectQuery $query, array $outerKeys = []): SelectQuery
4462
{
4563
if ($this->options['using'] !== null) {
@@ -56,6 +74,13 @@ public function configureQuery(SelectQuery $query, array $outerKeys = []): Selec
5674
$this->options['where'] ?? $this->schema[Relation::WHERE] ?? [],
5775
);
5876

77+
// user specified ORDER_BY rules
78+
$this->setOrderBy(
79+
$query,
80+
$this->getAlias(),
81+
$this->options['orderBy'] ?? $this->schema[Relation::ORDER_BY] ?? [],
82+
);
83+
5984
return parent::configureQuery($query);
6085
}
6186

tests/ORM/Fixtures/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class User implements ImagedInterface
2828
*/
2929
public $lastComment;
3030

31+
/**
32+
* @var Comment|null
33+
*/
34+
public $firstComment;
35+
3136
/**
3237
* @var Collection|Comment[]
3338
*/

0 commit comments

Comments
 (0)