Builder::find_all( mixed|TECEventsCustom_TablesV1Modelsarray $value, string|null $column = null )
Finds all the Model instances matching a set of values for a column.
Contents
The method will query the database for matching Models in batches of fixed size that will be hidden from the client code.
Parameters
- $value
-
(mixed|<span class="TECEventsCustom_TablesV1Modelsarray">TECEventsCustom_TablesV1Modelsarray) (Required) The value, or values, to find the matches for.
- $column
-
(string|null) (Optional) The column to search the Models by, or
nullto use the Model primary column.Default value: null
Return
(TECEventsCustom_TablesV1ModelsGenerator<Model>|null) A generator that will return all matching Model instances hiding the batched query logic.
Source
File: src/Events/Custom_Tables/V1/Models/Builder.php
public function find_all( $value, $column = null ) {
if ( false === $column_data_format = $this->check_find_value_column( $value, $column ) ) {
// Nothing to return.
return;
}
$this->query = '';
list( $column, $data, $format ) = $column_data_format;
$operator = is_array( $value ) ? 'IN' : '=';
$compare = is_array( $value ) ? implode( ',', array_column( $format, $column ) ) : $format[ $column ];
$data = is_array( $value ) ? array_column( $data, $column ) : $data;
$orderBy = ! empty( $this->order ) ? 'ORDER BY `' . $this->order['column'] . '` ' . $this->order['order'] : '';
global $wpdb;
$SQL = "SELECT * FROM {$wpdb->prefix}{$this->model->table_name()} WHERE `{$column}` {$operator} ({$compare}) {$orderBy} LIMIT %d";
$batch_size = min( absint( $this->batch_size ), 5000 );
$semi_prepared = $wpdb->prepare( $SQL, array_merge( (array) $data, [ $batch_size ] ) );
$model_class = get_class( $this->model );
// Start with no results.
$results = [];
$offset = 0;
$found = 0;
do {
if ( empty( $results ) ) {
// Run a fetch if we're out of results to return, maybe get some results.
$results = $wpdb->get_results( $semi_prepared . " OFFSET {$offset}", ARRAY_A );
$offset += $batch_size;
$found = count( $results );
$results = array_reverse( $results );
}
// Get a result from the fetch.
$result = array_pop( $results );
if ( null === $result ) {
// No more results.
break;
}
// Yield a model instance.
yield new $model_class( $result );
} while ( $found > 0 );
// We're done.
return;
}
Changelog
| Version | Description |
|---|---|
| 6.0.0 | Introduced. |