Bouncy Melons 0.2
declarative style orm
basic-usage.php
<?php
namespace bm\example;
/*
* basic usage of Bouncy Melons
*/
/*
* lets declare data structure first
*/
class VMyPhoneBook extends \bm\VModel {
/*
* declare fields, id field is already on board, no need to mention it here
*/
public function declareFields() {
return [
new \bm\StringField('name'), // simple string field
new \bm\PhoneField('phone',null, ['mask'=>'+*(***)***-**-**']), // this is stored as string, but will be formatted using mask
];
}
}
/*
* init connection (and yes you can connect to diffent sql servers in same time)
*/
$bm = new \bm\BouncyMelons();
$bm->connect('mysql:dbname=bouncymelons;host=localhost','user','password');
/*
* let bouncy melons know about us
*/
$bm->park([
new VMyPhoneBook(),
]);
/*
* now we can work, lets start with CRUD
*/
/*
* store new phone in database
*/
$vm = $bm->getVmBySlug(VMyPhoneBook::getSlug());//get viewmodel instance
$list = $vm->getListModel(); // get list model
$item = $list->createSingle(); // create new single item
$item->setData(['name'=>'Sexy Girl','phone'=>'1111 222 5555']);// note that phone will be formatted automatically to 1(111)222-55-55
$id = $item->save();// create table and store new record
/*
* read singe record by id
*/
$item = $list->readOne($id);
$phone = $item->get('phone'); // $phone = 1(111)222-55-55
/*
* let's get phone number by girl's name
*/
$item = $list->readOne('Sexy Girl','name');
$phone = $item->get('phone');
/*
* read list of all girls
*/
$items = $list->find(new \bm\FilterAll());
/*
* get only sexy girls
*/
$items = $list->find(new \bm\FilterPermissive(['name'=>'Sexy']));
// match girls with string 'Sexy' in their name
$items = $list->find(new \bm\FilterStrict(['name'=>'Sexy']));
// match girls wich names are 'Sexy' without any other characters
/*
* custom WHERE SQL query
*/
$items = $list->find(new \bm\FilterRaw("`name` LIKE 'Sexy%'"));
/*
* pagination, for single query
*/
$filter = new \bm\FilterPermissive(['name'=>'Sexy'],
$pageNumber,/*optional default 0*/
$itemsPerPage/*optional default 50*/
);
$items = $list->find($filter);
$total = $list->getTotal($filter);// how many records for this query
$pages = $list->getMaxPages($filter);// how many pages we have
/*
* get all at once
*/
list($items, $total, $pages) = $list->fullfind($filter);
/*
* pagination, for entire project
*/
$bm->setItemsPerPage(7);// set items per page for all queries
list($items, $total, $pages) = $list->fullfind(new \bm\FilterPermissive(['name'=>'Sexy']));
/*
* for more complex queries read about DataList::declareView() and/or VModel::declareView()
* see list.php and view-model-extended.php examples
*/