Skip to content

Commit

Permalink
new function Table::get()
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 8, 2020
1 parent 69c9abe commit eddaa22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 14 additions & 11 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down

4 comments on commit eddaa22

@akhilkazi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello sir,
need some help i have database working on local xampp server. it want to connect it to live server. please reply on [email protected]

@oscarotero
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akhilkazi This is not related with this library. Better ask in stackoverflow.

@akhilkazi
Copy link

@akhilkazi akhilkazi commented on eddaa22 Aug 9, 2020 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akhilkazi
Copy link

@akhilkazi akhilkazi commented on eddaa22 Aug 9, 2020 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.