44
55namespace Cycle \ORM \Select \Loader ;
66
7+ use Cycle \Database \Query \SelectQuery ;
8+ use Cycle \ORM \FactoryInterface ;
79use Cycle \ORM \Parser \AbstractNode ;
810use Cycle \ORM \Parser \SingularNode ;
11+ use Cycle \ORM \Service \SourceProviderInterface ;
912use Cycle \ORM \Relation ;
1013use Cycle \ORM \SchemaInterface ;
1114use Cycle \ORM \Select \JoinableLoader ;
1215use Cycle \ORM \Select \Traits \JoinOneTableTrait ;
16+ use Cycle \ORM \Select \Traits \OrderByTrait ;
1317use 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.
2528class 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
0 commit comments