2017 © Pedro Peláez
 

library db

ORM

image

vivace/db

ORM

  • Wednesday, May 16, 2018
  • by KPEMATOP
  • Repository
  • 2 Watchers
  • 6 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

vivace\db

Latest Stable Version Total Downloads License composer.lock Maintainability, (*1)

Goals

Create a flexible query builder with relationship support and CRUD operations., (*2)

Requirements

  • php >= 7.1

Supported databases

  • [x] PostgreSQL >=9.5
  • [x] MySQL >= 5.7
  • [ ] MongoDB (planned)

Installing

composer require vivace/db

Usage

Initialize driver for your database. In this example, the driver for postgresql., (*3)

$pdo = new \PDO('dsn', 'user', 'pass');
$driver = new \vivace\db\sql\PostrgeSQL\Driver($pdo);

Initialize storage objects., (*4)

$userStorage = new \vivace\db\sql\Storage($driver, 'users');

Now you can use created storages for data manipulation., (*5)

Save the data to your storage., (*6)

$ok = $userStorage->save(['name' => 'Zoe Saldana', 'career' => 'actor', 'rating' => 4.95]);

Let's try fetch saved data from storage., (*7)

$user = $userStorage->filter(['name' => 'Zoe Saldana'])->fetch()->one();
// $user is simple assoc array.
var_dump($user);

Now it's time to change the data, (*8)

$user['age'] = 39;

And save changes in storage., (*9)

$ok = $userStorage->save($user);

More examples.

Filtering.

$users = $userStorage
    ->limit(100)
    // equalent SQL condition "career IN ('actor', 'producer') OR age >= 40"
    ->filter(['or', ['in', 'career', ['actor', 'producer'], ['>=', 'age', 40]])
    ->fetch();

Insert/Update

Insert one row, (*10)

$ok = $userStorage->save(['name' => 'Mark Rufallo']);

Multiple rows, (*11)

$ok = $userStorage->save([
    ['name' => 'Mark Rufallo'],
    ['name' => 'Chris Evans', 'rating' => 4.95],
]);

Update if exists, otherwise insert, (*12)

$ok = $userStorage->save([
    ['id' => 6, 'name' => 'Mark Ruffalo'],// This data will be updated, because 'id' is the primary key
    ['name' => 'Chris Hemsworth'] // This data will be inserted as new row
]);

Update by condition.

$numberOfupdated = $userStorage
    ->sort(['id' => -1])// Sorting by `id` in descending order
    ->skip(100)// skip first 100 found rows
    ->update(['career' => 'actor']);

Delete by condition.

$numberOfDeleted = $userStorage
    ->filter(['age' => 18, 'rating' => 5])
    ->and(['in', 'career', ['actor', 'producer'])
    // Delete all users by condition "age = 18 AND rating = 5 AND career IN ('actor', 'producer')"
    ->delete();

Relations.

$userStorage = $userStorage->projection([
    // OneToOne
    'country' => new \vivace\Relation\Single($countryStorage, ['country_id' => 'id']),
    // OneToMany
    'rewards' => new \vivace\Relation\Many($rewardsStorage, ['id' => 'user_id'])
]);

$users = $userStorage->fetch()->all();

foreach($users as $user){
    if(isset($user['country'])) {
        var_dump($user['country']);
    }
    foreach($user['rewards'] as $reward){
        var_dump($reward);        
    }
}

Field aliases.

$userStorage = $userStorage->projection([
    'rank' => 'rating'
]);

// Aliases are available for use in the condition.
$user = $userStorage->filter(['between', 'rank', 4, 5])->fetch()->one();

Running the tests

For tests, you need to connect to a database. If you use a docker, then you can raise the database with the following command:, (*13)

docker-compose up -d mysql pgsql

And run tests:, (*14)

docker-compose run --rm php codecept run --env pgsql --env mysql

The Versions

16/05 2018

dev-master

9999999-dev

ORM

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

orm database postgres mysql builder