2017 © Pedro Peláez
 

library databean

DataBean is a simple and testable database access layer for PHP.

image

buyplaytix/databean

DataBean is a simple and testable database access layer for PHP.

  • Friday, July 13, 2018
  • by tthomas48
  • Repository
  • 1 Watchers
  • 0 Stars
  • 638 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 20 Versions
  • 6 % Grown

The README.md

DataBean

DataBean is a simple and testable database access layer for PHP. It is easy to write tests and create simple beans for accessing and updating the database., (*1)

Setup

Setup is simple, (*2)

\BuyPlayTix\DataBean\DB::init(array(
    "user" => $database_username,
    "pass" => $database_password,
    "name" => $database_name,
    "host" => $database_host,
));
// (optional) pass in a logger that implements IDBLogger
\BuyPlayTix\DataBean\DB::setLogger($logger);

Creating a Bean

A bean maps directly to a table. Setting up a bean is simple:, (*3)

class Message extends \BuyPlayTix\DataBean\DataBean
{
  protected $table = 'MESSAGES';
  protected $pk = 'UID';
}

Interacting with a Bean

Loading a bean

// create a new bean
$message = new Message();

// load a bean by primary key
$message = new Message($uid);

// load a bean by a unique key
$message = new Message(array("SUBJECT", $subject));

Loading multiple beans

// load by a key
$messages = Message::getObjects("OWNER_UID", $owner_uid);

// load by a key with values
$messages = Message::getObjects("OWNER_UID", array($owner_uid1, $owner_uid2, $owner_uid3));    

// load by a key with ordering
$messages = Message::getObjects("OWNER_UID", $owner_uid, " ORDER BY INSERT_TIMESTAMP ");

Updating

$message->OWNER_UID = $new_owner_uid;
$message->update();

Deleting

$message->delete();

Arbitrary SQL

Why would you want to do this rather than directly running against the database? One word - "testing"., (*4)

// select
$messages = raw_select("MESSAGES", array("UID", "SUBJECT", "BODY"), array("OWNER_UID" => $owner_uid));

// update
raw_update("MESSAGES", array("OWNER_UID" => $new_owner_uid), array("OWNER_UID => $old_owner_uid));

// delete
raw_delete("MESSAGES", array("OWNER_UID => $owner_uid));

// complex query
$results = named_query("MESSAGES_BY_HOUR", "select date_format(INSERT_TIMESTAMP, '%H'), count(*) from MESSAGES group by date_format(INSERT_TIMESTAMP, '%H')");

Testing

Setting up a Test

\BuyPlayTix\DataBean\DB::setInstance(\BuyPlayTix\DataBean\NullDB::getInstance());
\BuyPlayTix\DataBean\DataBean::setAdapter(new \BuyPlayTix\DataBean\ObjectAdapter());

A Simple Test

$message = new Message();
$message->SUBJECT = "A Test Message";
$message->update();

assertEquals(1, Message::getObjects("SUBJECT", array("A Test Message"));

A More Complex Test

Let's say that we want to test the method get_queries_by_hour defined like this on our message databean:, (*5)

function get_queries_by_hour() {
  return named_query("MESSAGES_BY_HOUR", "select date_format(INSERT_TIMESTAMP, '%H'), count(*) from MESSAGES group by date_format(INSERT_TIMESTAMP, '%H')");
}


$adapter = \BuyPlayTix\DataBean\DataBean::getAdapter();
$adapter->set_named_query_value("MESSAGES_BY_HOUR", array(array("1", 200), array("2", 300));
$results = $message->get_queries_by_hour();
assertEquals(2, count($results));

The Versions

13/07 2018

dev-master

9999999-dev

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

13/07 2018

0.6.7

0.6.7.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

10/05 2018

0.6.5

0.6.5.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

06/05 2018

0.6.4

0.6.4.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

15/08 2017

0.6.3

0.6.3.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

28/07 2017

0.6.2

0.6.2.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

28/07 2017

0.6.1

0.6.1.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

27/07 2017

0.6.0

0.6.0.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

18/07 2017

0.5.7

0.5.7.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

17/07 2017

0.5.6

0.5.6.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

05/07 2017

0.5.5

0.5.5.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

04/07 2017

0.5.4

0.5.4.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

21/11 2016

0.5.3

0.5.3.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

03/09 2016

0.5.2

0.5.2.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

21/12 2015

0.5.0

0.5.0.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

12/06 2015

0.4.10

0.4.10.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

 

14/05 2015

0.4.6

0.4.6.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.2
  • ext-uuid *

 

22/04 2015

0.4.5

0.4.5.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.2
  • ext-uuid *

 

17/04 2015

0.4.4

0.4.4.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.2
  • ext-uuid *

 

04/11 2014

0.4.3

0.4.3.0

DataBean is a simple and testable database access layer for PHP.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.2
  • ext-uuid *