2020use Cycle \ORM \Schema ;
2121use Cycle \ORM \Select \Traits \AliasTrait ;
2222use Cycle \ORM \Select \Traits \ChainTrait ;
23+ use Cycle \ORM \Select \Traits \ScopeTrait ;
2324use Spiral \Database \Query \SelectQuery ;
2425
2526/**
@@ -45,23 +46,24 @@ abstract class AbstractLoader implements LoaderInterface
4546{
4647 use ChainTrait;
4748 use AliasTrait;
49+ use ScopeTrait;
4850
4951 // Loading methods for data loaders.
50- public const INLOAD = 1 ;
51- public const POSTLOAD = 2 ;
52- public const JOIN = 3 ;
52+ public const INLOAD = 1 ;
53+ public const POSTLOAD = 2 ;
54+ public const JOIN = 3 ;
5355 public const LEFT_JOIN = 4 ;
5456
55- /** @var ORMInterface|SourceProviderInterface @internal */
57+ /** @var ORMInterface @internal */
5658 protected $ orm ;
5759
5860 /** @var string */
5961 protected $ target ;
6062
6163 /** @var array */
6264 protected $ options = [
63- 'load ' => false ,
64- 'constrain ' => true ,
65+ 'load ' => false ,
66+ 'scope ' => true ,
6567 ];
6668
6769 /** @var LoaderInterface[] */
@@ -75,12 +77,14 @@ abstract class AbstractLoader implements LoaderInterface
7577
7678 /**
7779 * @param ORMInterface $orm
78- * @param string $target
80+ * @param string $target
7981 */
8082 public function __construct (ORMInterface $ orm , string $ target )
8183 {
8284 $ this ->orm = $ orm ;
8385 $ this ->target = $ target ;
86+
87+ $ this ->setScope ($ this ->getSource ()->getScope ());
8488 }
8589
8690 /**
@@ -132,6 +136,8 @@ public function getSource(): SourceInterface
132136 */
133137 public function withContext (LoaderInterface $ parent , array $ options = []): LoaderInterface
134138 {
139+ $ options = $ this ->prepareOptions ($ options );
140+
135141 // check that given options are known
136142 if (!empty ($ wrong = array_diff (array_keys ($ options ), array_keys ($ this ->options )))) {
137143 throw new LoaderException (
@@ -154,9 +160,9 @@ public function withContext(LoaderInterface $parent, array $options = []): Loade
154160 * Load the relation.
155161 *
156162 * @param string $relation Relation name, or chain of relations separated by.
157- * @param array $options Loader options (to be applied to last chain element only).
158- * @param bool $join When set to true loaders will be forced into JOIN mode.
159- * @param bool $load Load relation data.
163+ * @param array $options Loader options (to be applied to last chain element only).
164+ * @param bool $join When set to true loaders will be forced into JOIN mode.
165+ * @param bool $load Load relation data.
160166 * @return LoaderInterface Must return loader for a requested relation.
161167 *
162168 * @throws LoaderException
@@ -279,7 +285,7 @@ abstract protected function initNode(): AbstractNode;
279285 */
280286 protected function configureQuery (SelectQuery $ query ): SelectQuery
281287 {
282- $ query = $ this ->applyConstrain ($ query );
288+ $ query = $ this ->applyScope ($ query );
283289
284290 foreach ($ this ->join as $ loader ) {
285291 $ query = $ loader ->configureQuery ($ query );
@@ -294,12 +300,6 @@ protected function configureQuery(SelectQuery $query): SelectQuery
294300 return $ query ;
295301 }
296302
297- /**
298- * @param SelectQuery $query
299- * @return SelectQuery
300- */
301- abstract protected function applyConstrain (SelectQuery $ query ): SelectQuery ;
302-
303303 /**
304304 * Define schema option associated with the entity.
305305 *
@@ -325,4 +325,14 @@ protected function getEagerRelations(): \Generator
325325 }
326326 }
327327 }
328+
329+ protected function prepareOptions (array $ options ): array
330+ {
331+ if (array_key_exists ('constrain ' , $ options ) && !array_key_exists ('scope ' , $ options )) {
332+ $ options ['scope ' ] = $ options ['constrain ' ];
333+ }
334+ unset($ options ['constrain ' ]);
335+
336+ return $ options ;
337+ }
328338}
0 commit comments