mirror of
https://codeberg.org/vlw/php-mysql.git
synced 2025-09-14 00:33:41 +02:00
feat: accept CSV string as columns for select()
(#16)
* feat: accept CSV string as columns for select() * feat(doc): add string doc for select to README
This commit is contained in:
parent
5abcb48010
commit
13720e772e
2 changed files with 9 additions and 5 deletions
|
@ -59,13 +59,13 @@ $db = new MySQL($host, $user, $pass, $db);
|
||||||
|
|
||||||
# SELECT
|
# SELECT
|
||||||
|
|
||||||
Use `MySQL->select()` to retrieve columns from a database table
|
Use `MySQL->select()` to retrieve columns from a database table.
|
||||||
|
|
||||||
|
Pass an associative array of strings, CSV string, or null to this method to filter columns.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$db->select(
|
$db->select(
|
||||||
// Sequential array of string with column names to retrieve
|
array|string|null $columns
|
||||||
// Or null to retrieve a bool if rows were matched
|
|
||||||
?array $columns
|
|
||||||
): array|bool;
|
): array|bool;
|
||||||
// Returns array of arrays for each row, or bool if no columns were defined
|
// Returns array of arrays for each row, or bool if no columns were defined
|
||||||
```
|
```
|
||||||
|
@ -75,6 +75,7 @@ In most cases you probably want to select with a constraint. Chain the [`where()
|
||||||
### Example
|
### Example
|
||||||
```php
|
```php
|
||||||
$beverages = $db->for("beverages")->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages
|
$beverages = $db->for("beverages")->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages
|
||||||
|
$beverages = $db->for("beverages")->select("beverage_name, beverage_size"); // SELECT beverage_name, beverage_size FROM beverages
|
||||||
```
|
```
|
||||||
```
|
```
|
||||||
[
|
[
|
||||||
|
|
|
@ -136,9 +136,12 @@
|
||||||
/* ---- */
|
/* ---- */
|
||||||
|
|
||||||
// Create Prepared Statament for SELECT with optional WHERE filters
|
// Create Prepared Statament for SELECT with optional WHERE filters
|
||||||
public function select(?array $columns = null): array|bool {
|
public function select(array|string|null $columns = null): array|bool {
|
||||||
$this->throw_if_no_table();
|
$this->throw_if_no_table();
|
||||||
|
|
||||||
|
// Create array of columns from CSV
|
||||||
|
$columns = is_array($columns) ? $columns : explode(",", $columns);
|
||||||
|
|
||||||
// Filter columns that aren't in the model if defiend
|
// Filter columns that aren't in the model if defiend
|
||||||
if ($columns && $this->model) {
|
if ($columns && $this->model) {
|
||||||
$columns = $this->in_model($columns);
|
$columns = $this->in_model($columns);
|
||||||
|
|
Loading…
Add table
Reference in a new issue