Bouncy Melons 0.2
declarative style orm
basic-usage.php
Go to the documentation of this file.
1 <?php
2 namespace bm\example;
3 /*
4  * basic usage of Bouncy Melons
5  */
6 
7 /*
8  * lets declare data structure first
9  */
10 class VMyPhoneBook extends \bm\VModel {
11 
12  /*
13  * declare fields, id field is already on board, no need to mention it here
14  */
15  public function declareFields() {
16  return [
17  new \bm\StringField('name'), // simple string field
18  new \bm\PhoneField('phone',null, ['mask'=>'+*(***)***-**-**']), // this is stored as string, but will be formatted using mask
19  ];
20  }
21 }
22 
23 /*
24  * init connection (and yes you can connect to diffent sql servers in same time)
25  */
26 $bm = new \bm\BouncyMelons();
27 $bm->connect('mysql:dbname=bouncymelons;host=localhost','user','password');
28 
29 /*
30  * let bouncy melons know about us
31  */
32 $bm->park([
33  new VMyPhoneBook(),
34 ]);
35 
36 /*
37  * now we can work, lets start with CRUD
38  */
39 
40 /*
41  * store new phone in database
42  */
43 $vm = $bm->getVmBySlug(VMyPhoneBook::getSlug());//get viewmodel instance
44 $list = $vm->getListModel(); // get list model
45 $item = $list->createSingle(); // create new single item
46 $item->setData(['name'=>'Sexy Girl','phone'=>'1111 222 5555']);// note that phone will be formatted automatically to 1(111)222-55-55
47 $id = $item->save();// create table and store new record
48 
49 /*
50  * read singe record by id
51  */
52 $item = $list->readOne($id);
53 $phone = $item->get('phone'); // $phone = 1(111)222-55-55
54 
55 /*
56  * let's get phone number by girl's name
57  */
58 $item = $list->readOne('Sexy Girl','name');
59 $phone = $item->get('phone');
60 
61 /*
62  * read list of all girls
63  */
64 $items = $list->find(new \bm\FilterAll());
65 
66 /*
67  * get only sexy girls
68  */
69 $items = $list->find(new \bm\FilterPermissive(['name'=>'Sexy']));
70 // match girls with string 'Sexy' in their name
71 
72 $items = $list->find(new \bm\FilterStrict(['name'=>'Sexy']));
73 // match girls wich names are 'Sexy' without any other characters
74 
75 /*
76  * custom WHERE SQL query
77  */
78 $items = $list->find(new \bm\FilterRaw("`name` LIKE 'Sexy%'"));
79 
80 /*
81  * pagination, for single query
82  */
85 $filter = new \bm\FilterPermissive(['name'=>'Sexy'],
86  $pageNumber,/*optional default 0*/
87  $itemsPerPage/*optional default 50*/
88  );
89 $items = $list->find($filter);
90 $total = $list->getTotal($filter);// how many records for this query
91 $pages = $list->getMaxPages($filter);// how many pages we have
92 
93 
94 /*
95  * get all at once
96  */
97 list($items, $total, $pages) = $list->fullfind($filter);
98 
99 /*
100  * pagination, for entire project
101  */
102 $bm->setItemsPerPage(7);// set items per page for all queries
103 list($items, $total, $pages) = $list->fullfind(new \bm\FilterPermissive(['name'=>'Sexy']));
104 
105 /*
106  * for more complex queries read about DataList::declareView() and/or VModel::declareView()
107  * see list.php and view-model-extended.php examples
108  */
static getSlug()
Definition: VModel.php:48