Skip to content

jworksuk/extra-pdo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ExtraPDO

Latest Version on Packagist Software License Build Status

A very thin layer over the PDO class.

Install

Via Composer

$ composer require jworksuk/extra-pdo

Usage

require_once '/path/to/vendor/autoload.php';

use JWorksUK\ExtraPDO;
use Acme\Model;

$pdo = ExtraPDO::createMySqlConnection(
    'databaseName',
    'databaseHost',
    'databaseUser',
    'databasePassword'
);

$model = $pdo
    ->executeStatement('SELECT * FROM todos WHERE id=:id', [
        'id' => '15983acf-022f-303a-bfef-a096eaebbf7c'
    ])
    ->fetchAndMap(function ($row) {
        return new Model(
            $row['id'],
            $row['name'],
            $row['list_id'],
            new \DateTime($row['updated_at']),
            new \DateTime($row['created_at'])
        );
    });
$model->doModelThing();

$models = $pdo
    ->executeStatement('SELECT * FROM todos WHERE list_id=:listId', [
        'listId' => '05aab6f6-e991-3c59-a980-832cca75c578'
    ])
    ->fetchAllAndMap(function ($row) {
        return new Model(
            $row['id'],
            $row['name'],
            $row['list_id'],
            new \DateTime($row['updated_at']),
            new \DateTime($row['created_at'])
        );
    });

foreach ($models as $model) {
    $model->doModelThing();
}

Testing

$ composer test