graze / dal

Data Access Layer
MIT License
8 stars 0 forks source link

Add fetch() and fetchOne() to AdapterInterface to expose querying to repositories #22

Closed wpillar closed 9 years ago

wpillar commented 9 years ago

This adds fetch() and fetchOne() methods to AdapterInterface so we can expose the ability to directly query the DB to repositories.

(Also contains some opportunistic tidying)

Fixes #21.

Test Code

<?php

use Doctrine\ORM\Tools\Setup;
use Graze\Dal\Adapter\AdapterInterface;

require 'vendor/autoload.php';

$capsule = new \Illuminate\Database\Capsule\Manager();

$capsule->addConnection([
    'driver'      => 'mysql',
    'host'        => 'localhost',
    'unix_socket' => '/var/lib/mysql/mysql.live.sock',
    'database'    => 'live_admin',
    'username'    => 'will',
    'password'    => '',
    'charset'     => 'utf8',
    'collation'   => 'utf8_unicode_ci',
    'prefix'      => ''
]);
$capsule->bootEloquent();

$conn = [
    'driver'      => 'pdo_mysql',
    'host'        => 'localhost',
    'unix_socket' => '/var/lib/mysql/mysql.live.sock',
    'dbname'      => 'live_admin',
    'user'        => 'will',
    'password'    => '',
    'charset'     => 'utf8',
    'collation'   => 'utf8_unicode_ci',
    'prefix'      => ''
];

$config = Setup::createYAMLMetadataConfiguration([]);

/**
 * @var AdapterInterface[] $adapters
 */
$adapters = [
    new \Graze\Dal\Adapter\EloquentOrmAdapter($capsule->getConnection(), new \Graze\Dal\Adapter\EloquentOrm\Configuration([])),
    new \Graze\Dal\Adapter\DoctrineOrmAdapter(\Doctrine\ORM\EntityManager::create($conn, $config))
];

foreach ($adapters as $adapter) {
    dump($adapter->fetch('SELECT * FROM user_level WHERE ul_id = ?', [2]));
    dump($adapter->fetchOne('SELECT * FROM user_level WHERE ul_id = ?', [2]));
}

Output

// eloquent orm
array:1 [
  0 => array:6 [
    "ul_id" => "2"
    "ul_name" => "admin"
    "ul_level" => "3"
    "ul_updated" => "2011-04-19 15:50:26"
    "ul_added" => "2010-09-10 10:35:32"
    "ul_deleted" => "2011-04-19 15:50:26"
  ]
]
array:6 [
  "ul_id" => "2"
  "ul_name" => "admin"
  "ul_level" => "3"
  "ul_updated" => "2011-04-19 15:50:26"
  "ul_added" => "2010-09-10 10:35:32"
  "ul_deleted" => "2011-04-19 15:50:26"
]

// doctrine orm
array:1 [
  0 => array:6 [
    "ul_id" => "2"
    "ul_name" => "admin"
    "ul_level" => "3"
    "ul_updated" => "2011-04-19 15:50:26"
    "ul_added" => "2010-09-10 10:35:32"
    "ul_deleted" => "2011-04-19 15:50:26"
  ]
]
array:6 [
  "ul_id" => "2"
  "ul_name" => "admin"
  "ul_level" => "3"
  "ul_updated" => "2011-04-19 15:50:26"
  "ul_added" => "2010-09-10 10:35:32"
  "ul_deleted" => "2011-04-19 15:50:26"
]
john-n-smith commented 9 years ago

Need to ask you what a Capsule is, but functional code looks good :+1:

wpillar commented 9 years ago

Capsule is just Eloquent's name for a thing that holds connections. I don't really know why it's called that lol.

wpillar commented 9 years ago

Happy for me to merge then?

john-n-smith commented 9 years ago

Go for it!

On 20 May 2015 at 12:52, Will Pillar notifications@github.com wrote:

Happy for me to merge then?

— Reply to this email directly or view it on GitHub https://github.com/graze/dal/pull/22#issuecomment-103856669.

John Smith

Operations Developer

email john@graze.com

phone +44 20 3597 8344

graze