Bone

A Bone is a handy tool to create your database entities. Consider the next example. It makes use of the redBeansPHP library: http://www.redbeanphp.com

This will create a table "customer" with 5 columns. It will then also create the activerecord for the table.

  • id (autoincrease,primary)
  • username
  • password
  • email
  • created

CreateBone

// Create a Bone.
Barebone\ORM::CreateBone('customer', [
    'username' => 'Tester',
    'password' => md5('123456verysecurypassword'),
    'email' => 'frank@test.nl',
    'created' => date('Y-m-d H:i:s')
]);

// activerecord is now available
$customer = new customer();
$this->debug( $customer->all() );    

The ORM creates the necessary tables and columns automagically. The type of the column in the database depends on the value you want to store in it. The ORM scans the value you want to store in a column and makes sure the column has a type that can contain your data properly. You can always tune your database schema manually of course.

CreateBone

ReadBone

// Read a Bone.
$customer = Barebone\ORM::fetchBone( 'customer', 1 );

echo $customer->username;

$this->debug($customer);  // shows the bean

ReadBone

UpdateBone

// update the bone with id 1 and add a column for the updated timestamp
Barebone\ORM::UpdateBone('customer', 1, [
    'username' => 'Testertjes',
    'password' => md5('123456verysecurypassword'),
    'email' => 'barebone@it-api.nl',
    'updated' => date('Y-m-d H:i:s')
]);

// activerecord is available
$customer = new customer();
$this->debug( $customer->all() );    

UpdateBone

CrushBone(s)

// delete the bone with id 1
Barebone\ORM::CrushBone('customer', 1);

// delete the whole table, be carefull!!
Barebone\ORM::CrushBones('customer');
CrushBone (remover row with id 1) / CrushBones (removes the table!!)

One-to-many

In a one-to-many relation, one bean has a list of other bones but all those bones cannot belong to another bean at the same time. For instance, let's create a shop:

$shop = Barebone\ORM::dispense( 'shop' );
$shop->name = 'Antiques';

To add products to the shop, add bones to the ownProductList property, like this:

$vase = Barebone\ORM::dispense( 'product' );
$vase->price = 25;
$shop->ownProductList[] = $vase
Barebone\ORM::store( $shop );

Each product in the ownProductList belongs to shop and cannot belong to another shop.

Note that the name of the list has to match the type of bones it contains. So, the 'ownProductList' contains bones of type 'product', a pageList contains pages, an 'ownCarList' contains 'cars' and so on. This convention is used to create the database mapping, in case of the shop, every product record will get a 'shop_id' field.

/**
 * create client table
 */
$client = Barebone\ORM::dispense( 'client' );
$client->initialen = 'A.B.';
$client->achternaam = 'Tester';
/**
 * create 2 adresses
 */
$adres = Barebone\ORM::dispense( 'adres' );
$adres->straatnaam = 'Testweg aan de ring zuid in Amsterdam';
$adres->huisnummer = 123;
$adres->postcode = '1234AA';
$adres->woonplaats = 'Testdrop';
// connect the adres to the client
$client->ownAdresList[] = $adres;

// create another adres
$adres = Barebone\ORM::dispense( 'adres' );
$adres->straatnaam = 'Testweg aan de ring zuid in Amsterdam';
$adres->huisnummer = 121;
$adres->postcode = '1234AA';
$adres->woonplaats = 'Testdrop';
// connect the adres also to the client
$client->ownAdresList[] = $adres;
/**
 * create contract table
 */
$contract = Barebone\ORM::dispense( 'contract' );
$contract->product = 'energie';
$contract->orderdate = date('Y-m-d H:i:s');
$contract->begindate = date('Y-m-d H:i:s');
$contract->enddate = date('Y-m-d H:i:s');
// connect the contract to the client
$client->ownContractList[] = $contract;

// store everything
Barebone\ORM::store( $client );

// result will be 3 tables all with indexes and foreign keys defined

One 2 many

Debugging

//turns debugging ON (recommended way)
//Barebone\ORM::fancyDebug( TRUE );
Barebone\ORM::debug( TRUE, 2 ); //select MODE 2 to see parameters filled in
Barebone\ORM::fancyDebug();

Finding stuff

Finding stuff in the database is easy:

// OOP SQL with placehoder
$customers = Barebone\ORM::find('post', ' id < ?', [ 5 ] );
// return bones

Find bones

This will search for all customers have an id smaller then 5 and will return an array containing all the relevant bones as a result. As you see, we don't use a fancy query builder, just good old SQL. We like to keep things simple.

Besides using the find() functions, you can also use raw SQL queries:

// pure SQL with placehoder
$customer = Barebone\ORM::getAll(
    'SELECT * FROM customer WHERE id < ? ',
    [ 5 ] );
// return array

Get all bones

Helper function

$bone = Bone('Users');
$bone->Username = 'Tester';
$bone->Password = 'Admin';
$created = $bone->create();
debug($created); // returns the activerecord