Improved Search Functionality
This commit is contained in:
@ -637,10 +637,29 @@ class Database {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function search( $table, $column, $param ) {
|
||||
public function searchColumn( $table, $column, $param ) {
|
||||
return $this->action( 'SELECT *', $table, [$column, 'LIKE', '%' . $param . '%'] );
|
||||
}
|
||||
|
||||
public function search( $table, $columns, $param ) {
|
||||
if ( empty( $columns ) || ! is_array( $columns ) ) {
|
||||
Debug::log( 'No columns provided for search' );
|
||||
return [];
|
||||
}
|
||||
|
||||
$conditions = [];
|
||||
foreach ( $columns as $column ) {
|
||||
$conditions[] = $column;
|
||||
$conditions[] = 'LIKE';
|
||||
$conditions[] = '%' . $param . '%';
|
||||
$conditions[] = 'OR';
|
||||
}
|
||||
array_pop( $conditions );
|
||||
|
||||
return $this->action( 'SELECT *', $table, $conditions );
|
||||
// return $this->action( 'SELECT ' . implode( ',', $columns ), $table, $conditions ); // need to find a way to casually make this the default....
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects data from the database.
|
||||
*
|
||||
|
@ -17,8 +17,9 @@ use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Bedrock;
|
||||
|
||||
class DatabaseModel extends Model {
|
||||
public $databaseMatrix;
|
||||
public $tableName;
|
||||
public $databaseMatrix;
|
||||
public $searchFields;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
@ -190,4 +191,19 @@ class DatabaseModel extends Model {
|
||||
}
|
||||
return $this->filter( $data->results() );
|
||||
}
|
||||
|
||||
public function search($param) {
|
||||
if (empty($this->searchFields)) {
|
||||
Debug::log('searchFields is empty');
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = self::$db->search($this->tableName, $this->searchFields, $param);
|
||||
|
||||
if ( $result->count() ) {
|
||||
return $this->filter( $result->results() );
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user