Skip to content

Commit e7705d7

Browse files
authored
Merge pull request #45 from stellarwp/feat/adding-the-order-by-param-in-get-all-by
Adding the order by param in the get_all_by method
2 parents fa9f3e8 + bdfc133 commit e7705d7

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file. This project adhere to the [Semantic Versioning](http://semver.org/) standard.
44

5+
## [3.1.1] 2025-10-01
6+
7+
* Tweak - Fix the `get_all_by` method to accept an order by clause.
8+
9+
[3.1.1]: https://github.com/stellarwp/schema/releases/tag/3.1.1
10+
511
## [3.1.0] 2025-09-30
612

713
* Feature - Introduce new column types: `Blob_Column`, `Binary_Column`, and `Boolean_Column`.

src/Schema/Tables/Contracts/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @method static int get_total_items( array $args = [] )
3838
* @method static bool update_many( array $entries )
3939
* @method static array paginate( array $args, int $per_page = 20, int $page = 1, array $columns = [ '*' ], string $join_table = '', string $join_condition = '', array $selectable_joined_columns = [], string $output = 'OBJECT' )
40-
* @method static mixed[] get_all_by( string $column, $value, string $operator = '=', int $limit = 50 )
40+
* @method static mixed[] get_all_by( string $column, $value, string $operator = '=', int $limit = 50, string $order_by = '' )
4141
* @method static ?mixed get_first_by( string $column, $value )
4242
* @method static ?mixed get_by_id( $id )
4343
* @method static array operators()

src/Schema/Traits/Custom_Table_Query_Methods.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,24 +671,26 @@ protected static function get_join_parts( string $join_table, string $join_condi
671671
* Gets all models by a column.
672672
*
673673
* @since 3.0.0
674+
* @since 3.1.1 Added the $order_by parameter.
674675
*
675676
* @param string $column The column to get the models by.
676677
* @param mixed $value The value to get the models by.
677678
* @param string $operator The operator to use.
678679
* @param int $limit The limit of models to return.
680+
* @param string $order_by The order by clause to use.
679681
*
680682
* @return mixed[] The models, or an empty array if no models are found.
681683
*
682684
* @throws InvalidArgumentException If the column does not exist.
683685
*/
684-
public static function get_all_by( string $column, $value, string $operator = '=', int $limit = 50 ): ?array{
686+
public static function get_all_by( string $column, $value, string $operator = '=', int $limit = 50, string $order_by = '' ): ?array{
685687
[ $value, $placeholder ] = self::prepare_value_for_query( $column, $value );
686688

687689
$operator = strtoupper( $operator );
688690

689691
$database = Config::get_db();
690692
$results = [];
691-
foreach ( static::fetch_all_where( $database::prepare( "WHERE %i {$operator} {$placeholder}", ...array_filter( [ $column, $value ], static fn( $v ) => null !== $v ) ), $limit, ARRAY_A ) as $task_array ) {
693+
foreach ( static::fetch_all_where( $database::prepare( "WHERE %i {$operator} {$placeholder}", ...array_filter( [ $column, $value ], static fn( $v ) => null !== $v ) ), $limit, ARRAY_A, $order_by ) as $task_array ) {
692694
if ( empty( $task_array[ static::uid_column() ] ) ) {
693695
continue;
694696
}

0 commit comments

Comments
 (0)