From eddaa223556b9ddec18c073f6b827ea32fcd06c1 Mon Sep 17 00:00:00 2001 From: oscarotero Date: Wed, 8 Jan 2020 12:16:40 +0100 Subject: [PATCH] new function Table::get() --- CHANGELOG.md | 5 +++++ README.md | 10 ++++++---- src/Table.php | 25 ++++++++++++++----------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ddfcb3..7954b26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [7.4.0] - Unreleased ### Added - `SelectAggregate` query allows to set not only fields but anything (math operations, for instance) and save the result as a column +- `Select` query has the `whereSprintf` and `orWhereSprintf` modifiers. +- New method `Table::get` To return a row from a table + +### Deprecated +- Magic method to return rows from a table using a field. Use `$table->get(['slug' => 'value'])` instead `$table->slug('value')`. ## [7.3.6] - 2019-12-25 ### Added diff --git a/README.md b/README.md index 9009d35..e7e45d4 100644 --- a/README.md +++ b/README.md @@ -134,12 +134,12 @@ $db->post[] = [ $totalPost = count($db->post); ``` -### Select by other unique keys +### Select by other fields -If you want to select a row by other key than `id`, just use a method with the field name: +If you want to select a row by other key than `id`, just use the method `get`: ```php -$post = $db->post->slug('post-slug'); +$post = $db->post->get(['slug' => 'post-slug']); ``` ### Select or create @@ -288,8 +288,10 @@ Function | Description `orderBy` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) `catHaving` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) `where` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) -`orWhere` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) +`whereSprintf` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) `catWhere` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) +`orWhere` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) +`orWhereSprintf` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) `whereEquals` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) `limit` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) `offset` | [Atlas.Query Select()](http://atlasphp.io/cassini/query/select.html) diff --git a/src/Table.php b/src/Table.php index 4a0532b..79dc1f3 100644 --- a/src/Table.php +++ b/src/Table.php @@ -400,17 +400,18 @@ public function getFields() return $this->fields; } + /** + * @deprecated + */ public function __call(string $name, array $args): ?Row { - $field = $this->__get($name); - - return $this->select()->one()->where("{$field} = ", $args[0])->run(); + return $this->get([$name => $args[0]]); } /** - * Search a row with some values or create one if it does not exist + * Search a row with some values */ - public function getOrCreate(array $data): Row + public function get(array $data): ?Row { $query = $this->select()->one(); @@ -420,13 +421,15 @@ public function getOrCreate(array $data): Row $query->where("{$field} = ", $value); } - $row = $query->run(); - - if ($row) { - return $row; - } + return $query->run(); + } - return $this->create($data); + /** + * Search a row with some values or create one if it does not exist + */ + public function getOrCreate(array $data): Row + { + return $this->get($data) ?: $this->create($data); } public function create(array $data = []): Row