Repository-Entity-Storage pattern data access library
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
db
sample
src
test
.codeclimate.yml
.coveralls.yml
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml

README.md

Calgamo/DataStore, Repository-Entity-Storage pattern data access library

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

Description

Calgamo/DataStore is a Repository-Entity-Storage pattern data access library.

Feature

  • Simple Query Builder
  • SQL Logging(supports PSR-3 Logger Interface)
  • Lazy connection
  • DBMS-specific SQL(SqlProviderInterface)
  • Single/Composite primary key
  • Transaction(begin/rollback/commit/savepoint)
  • Repository-Entity-Storage(table model) pattern

Demo

Prerequisite

CREATE TABLE `fruits` (
  `id` int(11) NOT NULL,
  `name` varchar(32) NOT NULL,
  `weight` float NOT NULL,
  `quality` int(11) NOT NULL,
  `updated_date` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `fruits` (`id`, `name`, `weight`, `quality`, `updated_date`) VALUES
(1, 'orange', 0.3, 1, NULL),
(2, 'melon', 0.1, 5, NULL),
(3, 'kiwi', 0.1, 7, NULL),
(4, 'apple', 0.3, 10, NULL);

Database connection settings

// MySQL
$dsn = 'mysql:dbname=calgamo_db;host=localhost';
$db = new Database($dsn,'root','');
 
// Sqlite
$dsn = 'sqlite:/path/to/sqlite_db_file';
$db = new Database($dsn);

Exsample 1: Executing Raw SQL

use Calgamo\DataStore\Storage\Database\Database;

try{
    $dsn = 'mysql:dbname=calgamo_db;host=localhost';
    $db = new Database($dsn,'root','');

    $result = $db->sql('SELECT * FROM fruits WHERE name like ?', ['o%'])->findAll();
    echo json_encode($result);    // [{"id":1,"name":"orange","weight":0.3,"quality":1,"updated_date":null}]
}
catch(\Throwable $e){
    exit($e->getMessage());
}

Exsample 2: Executing Count SQL

try{
    ...
    $result = $db->sql('select count(*) from fruits where name like ?',['banana'])->count();
    echo json_encode($result);    // 1
}
catch(\Throwable $e){
    exit($e->getMessage());
}

Exsample 3: Executing Insert SQL

try{
    ...
    $sql = "INSERT INTO fruits(name, weight, quality, updated_date) VALUES('mango', 0.4, 11, NOW())";

    $result = $db->sql($sql)->execute();
    echo json_encode($result);    // {"last_inserted_id":"5","affected_rows":1}
}
catch(\Throwable $e){
    exit($e->getMessage());
}

Exsample 4: Query Builder & Join

try{
    ...
    $result = $db->select(['shop_name', 'name', 'stock'])
        ->from('shops', 's')
        ->leftJoin('fruits_stock', 'st.shop_id = s.id', 'st')
        ->leftJoin('fruits', 'st.fruits_id = f.id', 'f')
        ->where('name', 'name = ?', 'melon')
        ->query()
        ->findAll();
    echo json_encode($result);    // [{"shop_name":"Super Fruits Market","name":"melon","stock":5},{"shop_name":"Mega Fruits","name":"melon","stock":15}]
}
catch(\Throwable $e){
    exit($e->getMessage());
}

Exsample 5: Entity & Repository

try{
    ...
    // create repository object from storage
    $repository = new FruitsRepository($db_storage);

    // count entities
    echo $repository->count()->execute() . PHP_EOL;     // 4

    // insert and get one entity
    $fruits = $repository->save(FruitsEntity::newEntity([
        'name' => 'mango',
        'weight' =>  0.5,
        'quality' => 11
    ]));
    echo json_encode($fruits->getFieldValues(['id','name','updated_date'])) . PHP_EOL;    // {"id":5,"name":"mango","updated_date":{}}

    echo $repository->count()->execute() . PHP_EOL;     // 5

    // get one entity from repository
    $fruits = $repository->getFruits(5);
    echo json_encode($fruits->getFieldValues(['id','name','updated_date'])) . PHP_EOL;    // {"id":5,"name":"mango","updated_date":"2018-05-19 20:32:27"}

    // get multiple entities from repository
    $all_fruits = $repository->getAllFruits();
    $all_fruits = array_map(function(FruitsEntity $item){
        return $item->name;
    }, $all_fruits);
    echo json_encode($all_fruits) . PHP_EOL;    // ["orange","melon","kiwi","apple","mango"]

    // delete one entity
    $repository->deleteFruits(5);

    echo $repository->count()->execute() . PHP_EOL;     // 4
}
catch(\Throwable $e){
    exit($e->getMessage());
}

Usage

Requirement

PHP 7.0 or later

Installing Calgamo/DataStore

The recommended way to install Calgamo/DataStore is through Composer.

composer require calgamo/data-store

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

This library is licensed under the MIT license.

Author

stk2k

Disclaimer

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.