1111use craft \elements \ElementCollection ;
1212use craft \helpers \ElementHelper ;
1313use CraftCms \Cms \Database \Queries \Exceptions \ElementNotFoundException ;
14+ use CraftCms \Cms \Database \Queries \Exceptions \QueryAbortedException ;
1415use CraftCms \Cms \Database \Table ;
1516use CraftCms \Cms \Support \Arr ;
1617use CraftCms \Cms \Support \Typecast ;
3132use ReflectionException ;
3233use ReflectionMethod ;
3334use ReflectionProperty ;
35+ use RuntimeException ;
3436use Tpetry \QueryExpressions \Function \Conditional \Coalesce ;
3537use Tpetry \QueryExpressions \Language \Alias ;
3638use Twig \Markup ;
@@ -448,7 +450,11 @@ public function getModels(array|string $columns = ['*']): array
448450 return $ result ;
449451 }
450452
451- $ this ->applyBeforeQueryCallbacks ();
453+ try {
454+ $ this ->applyBeforeQueryCallbacks ();
455+ } catch (QueryAbortedException ) {
456+ return [];
457+ }
452458
453459 if ((int ) $ this ->queryCacheDuration >= 0 ) {
454460 $ result = DependencyCache::remember (
@@ -499,7 +505,11 @@ public function pluck($column, $key = null): Collection|array
499505 return collect ($ result )->pluck ($ column , $ key );
500506 }
501507
502- $ this ->applyBeforeQueryCallbacks ();
508+ try {
509+ $ this ->applyBeforeQueryCallbacks ();
510+ } catch (QueryAbortedException ) {
511+ return $ this ->asArray ? [] : new Collection ;
512+ }
503513
504514 $ column = $ this ->columnMap [$ column ] ?? $ column ;
505515
@@ -523,7 +533,11 @@ public function count($columns = '*'): int
523533 return count ($ result );
524534 }
525535
526- $ this ->applyBeforeQueryCallbacks ();
536+ try {
537+ $ this ->applyBeforeQueryCallbacks ();
538+ } catch (QueryAbortedException ) {
539+ return 0 ;
540+ }
527541
528542 $ eagerLoadedCount = $ this ->eagerLoad (count: true );
529543
@@ -593,7 +607,11 @@ public function applyAfterQueryCallbacks(mixed $result): mixed
593607 */
594608 public function cursor (): LazyCollection
595609 {
596- $ this ->applyBeforeQueryCallbacks ();
610+ try {
611+ $ this ->applyBeforeQueryCallbacks ();
612+ } catch (QueryAbortedException ) {
613+ return new LazyCollection ;
614+ }
597615
598616 return $ this ->query ->cursor ()->map (function ($ record ) {
599617 $ model = $ this ->createElement ((array ) $ record );
@@ -799,7 +817,11 @@ public function __call($method, $parameters): mixed
799817 }
800818
801819 if (in_array (strtolower ($ method ), $ this ->passthru )) {
802- $ this ->applyBeforeQueryCallbacks ();
820+ try {
821+ $ this ->applyBeforeQueryCallbacks ();
822+ } catch (QueryAbortedException ) {
823+ return null ;
824+ }
803825
804826 if ((int ) $ this ->queryCacheDuration >= 0 ) {
805827 return DependencyCache::remember (
@@ -928,11 +950,6 @@ public function applyBeforeQueryCallbacks(): void
928950
929951 protected function elementQueryBeforeQuery (): void
930952 {
931- // Give other classes a chance to make changes up front
932- /*if (!$this->beforePrepare()) {
933- throw new QueryAbortedException();
934- }*/
935-
936953 $ this ->applySelectParams ();
937954
938955 // If an element table was never joined in, explicitly filter based on the element type
@@ -1071,4 +1088,18 @@ private function resolveColumnMapping(string $key): string|array
10711088
10721089 return $ this ->columnMap [$ key ];
10731090 }
1091+
1092+ /**
1093+ * Throw an exception if the query doesn't have an orderBy clause.
1094+ *
1095+ * @return void
1096+ *
1097+ * @throws \RuntimeException
1098+ */
1099+ protected function enforceOrderBy ()
1100+ {
1101+ if (empty ($ this ->query ->orders ) && empty ($ this ->query ->unionOrders )) {
1102+ throw new RuntimeException ('You must specify an orderBy clause when using this function. ' );
1103+ }
1104+ }
10741105}
0 commit comments