8000 fetchPairs fix by feropeterko · Pull Request #4 · varhall/dbino · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fetchPairs fix #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* @method Model|null get(mixed $key) Returns row specified by primary key.
* @method Model|null fetch() Fetches single row object.
* @method mixed fetchField(?string $column = null) Fetches single field.
* @method array fetchPairs(string|int|null $key = null, string|int|null $value = null) Fetches all rows as associative array.
* @method Model[] fetchAll() Fetches all rows.
* @method array fetchAssoc(string $path) Fetches all rows and returns associative tree.
* @method Collection select(string $columns, ...$params) Adds select clause, more calls appends to the end.
Expand Down Expand Up @@ -132,17 +131,27 @@ protected function resync(array $backup): void

/// OVERRIDEN METHODS

/**
* Counts number of rows. If column is not provided returns count of result rows, otherwise runs new sql counting query.
*/
public function count(?string $column = null): int
{
return $this->table->count($column);
}

/**
* Sets limit clause, more calls rewrite old values.
*/
public function limit(?int $limit, ?int $offset = null)
{
$this->table->limit($limit, $offset);
return $this;
}

/**
* Adds where condition, more calls appends with AND.
* @param string|array $condition possibly containing ?
*/
public function where($condition, ...$params)
{
// replace Collection with Selection in ...$params
Expand All @@ -154,6 +163,13 @@ public function where($condition, ...$params)
return $this;
}

/**
* Fetches all rows as associative array.
*/
public function fetchPairs(string|int|\Closure|null $keyOrCallback = null, string|int|null $value = null): array
{
return $this->table->fetchPairs($keyOrCallback, $value);
}



Expand Down
30 changes: 30 additions & 0 deletions tests/cases/CollectionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,36 @@ class CollectionTest extends DatabaseTestCase
Assert::equal($expected, $data->map(function($item) { return $item->title; })->toArray());
}

public function testFetchPairs_object()
{
$expected = [
1 => 'PHP Tips & Tricks',
2 => 'MySQL Queries',
3 => 'Einfach JavaScript',
4 => 'Web programming',
5 => 'Oracle',
];

$data = $this->collection->fetchPairs('id');

Assert::equal($expected, array_map(function($item) { return $item->title; }, $data));
}

public function testFetchPairs_value()
{
$expected = [
1 => 'PHP Tips & Tricks',
2 => 'MySQL Queries',
3 => 'Einfach JavaScript',
4 => 'Web programming',
5 => 'Oracle',
];

$data = $this->collection->fetchPairs('id', 'title');

Assert::equal($expected, $data);
}

// ICollection methods

public function testEach()
Expand Down
Loading
0