https://github.com/phore/phore-dba
database abstraction layer
https://github.com/phore/phore-dba
Last synced: 5 months ago
JSON representation
database abstraction layer
- Host: GitHub
- URL: https://github.com/phore/phore-dba
- Owner: phore
- Created: 2018-07-24T09:43:35.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-08-08T08:05:08.000Z (almost 7 years ago)
- Last Synced: 2025-03-14T09:06:03.268Z (over 1 year ago)
- Language: PHP
- Size: 53.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Phore Dba (DatabaseAbstraction)
## TL;DR;
Phore-dba is a very simple Object-Relational Mapper for PHP 7.
It will map Objects to Tables using exact the same Names.
- It will work with mysqli, sql, and PDO in gerneral.
- Update only changed properties
- [See sqlite3 example](examples/sqlite-create-table.php)
## Installation
Install using composer:
```
composer require phore/dba
```
## Example
- Create as Entity Class `SomeEntity` and define `__META__` data
- Initialize a Sqlite Connection to `/path/to/sqlite.db3`
- Create a Table `SomeEntity`
- Insert a new Entity for `name: someName` and `company: SomeCompany`
- `query()` all Entities and map them back to Entity using `each(callable $fn)`
- `update()` each entity, then `delete()` it (hm - it's just a demo)
```php
class SomeEntity {
use Entity;
const __META__ = [ "primaryKey" => "id" ];
public $id;
public $name;
public $company;
}
$odb = PhoreDba::InitDSN("sqlite:/path/to/sqlite.db3");
// Use multi_query() to execute multiple Statements
$odb->multi_query ("CREATE TABLE IF NOT EXISTS SomeEntity (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
company TEXT
);");
$odb->insert(new SomeEntity(["name"=>"someName", "company"=>"SomeCompany"]));
$odb->query("SELECT * FROM SomeEntity WHERE name=?", ["UnescapedValue"])->each(
function (array $row) use ($odb) {
print_r ($entity = SomeEntity::Load($row["id"]);
$entity->name = "MyNewName";
$odb->update($entity);
$odb->delete($entity);
}
);
```
## Loading from Database
```php
$entity = $odb->load(SomeEntity::class, 103878);
```
or - with object casting (IDE Code-Completion):
```php
$entity = SomeEntity::Load(103878);
```
## Working with entities
Entities must be loaded calling the `load()` method, so the framework
can track changes. You should use `query()` only to retrieve Id's and
load the entities afterwards.
### Changed fields
```php
$entity = SomeEntity::Load(1234); // Shortcut
$entity->name = "new Name";
assert ($enetity->isChanged("name") === true)
```