<?php include '../db.php'; //Create pager for page 2 with 8 items per page $pager = new \db\pager(3, 8); //When we set total item count pager recalculates itself $pager->total(30); \db\debug($pager); \db\debug($pager->page, 'current page'); \db\debug($pager->pages, 'pages count'); \db\debug($pager->total, 'total items'); \db\debug($pager->count, 'item count per page'); \db\debug($pager->from, 'first item number for result');
//in this sample we will learn how to modify field settings //it is done by documenting class and class properties //it is improtant that documentation section starts with line /** and ends with line */ include './000.config.php'; include '../db.php'; class user { /** * primary * length 11 * @var int */ public $id; /** * data char * length 100 * @var string */ public $name; } $database = new \db\database(); $database->link(new \db\link('default', $config->database, $config->username, $config->password)); $database->link('default')->debug = true; $database->default = 'db_samples'; $database->add('user'); //so if you are coming here from previous example //where user.name had not any configs you must have field 'name' in 'user' table char(128) //** and after runing update field 'name' will become char(100) because you specified it in length comment in public $name documentation $database->update(); \db\debug($database->user);
{ /** * primary * length 11 * @var int */ public $id; /** * data char * length 100 * @var string */ public $name; } $database = new \db\database('db_samples', new \db\link('default', $config->database, $config->username, $config->password)); $database->link('default')->debug = true; $database->add('user'); $database->update(); //create lazarus $lazarus = new \user(); $lazarus->name = 'Lazarus'; //save lazarus $database->user->save($lazarus); //kill lazarus unset($lazarus); //resurrect lazarus $lazarus = $database->user->load(1); \db\debug($lazarus); //1 stands for lazarus id //as soon as it is the first row in user table //it will have id 1 auto generated
<?php include '../db.php'; //here we define simple class class user { public $id; public $name; } //now lets see what happens when table eats the class: //pass class name to table constructor (in case of namespace //pass '\namespace1\namespace2\class_name' //or '.namespace1.namespace2.class_name') $table = new \db\table('user'); \db\debug($table);
$database->add('user'); $database->add('group'); $database->add('option'); $database->update(); $groups = $database->group->load(\db\by('name', 'User')); if ($groups) { $group = reset($groups); } else { $group = new group('User'); $database->save($group); } \db\debug($group); $query = new \db\query(); $query->order('name', 'asc'); $options = $database->option->load($query); if (!$options) { $options = array(); $options[] = new option('Option 1 in English', 'Option 2 in French'); $options[] = new option('Option 2 in English', 'Option 2 in French'); $options[] = new option('Option 3 in English', 'Option 2 in French'); $options[] = new option('Option 4 in English', 'Option 2 in French'); $database->save($options); } \db\debug($options); $user = new user('User ' . rand(1, 100)); $user->group = $group; $user->option[] = reset($options); $user->option[] = end($options); $database->save($user); \db\debug($user); $database->option->delete($options);
$database = new \db\database('db_samples', new \db\link('default', $config->database, $config->username, $config->password)); $database->link('default')->debug = true; $database->add('order'); $database->add('client'); $database->update(); $client = new client(); $client->name = 'Bill Gates'; $order = new order(); $order->name = 'Apple'; $order->client = $client; //the value of ready must be boolean //but we put 5 here $order->ready = 5; $order->income = 'there must be date time value'; //here must be an int $order->count = 1.5; //and here float $order->weight = true; //it will become string after load $order->worker = 10; //let's see all the mess inside order before save \db\debug($order); $database->client->save($client); $database->order->save($order); //object was modified during save //let us see how engine corrected values \db\debug($order); //let us see if something changed $order = $database->order->load($order->id); \db\debug($order); //create order
$database = new \db\database('db_samples', new \db\link('default', $config->database, $config->username, $config->password)); $database->link('default')->debug = true; //here we scan all the classes which name begins with '\core\' //you can use '.' instead of '\' if you wish //as we do: $database->scan('core'); //define locales for localized fields //first locale is treated as default locale $database->locales(array(new \db\locale('en'), new \db\locale('ge'))); $database->update(); //you can access class handler on $database->namespace->[namespace->]name $result = $database->core->solution->load(\db\by('name', 'blog')); if ($result) { $solution = reset($result); } else { $solution = new \core\solution('blog'); $database->save($solution); } $result = $database->core->project->load(\db\by('name', 'site')); if ($result) { $project = reset($result); } else { $project = new \core\project('site', $solution); $project->title_ge = 'საიტი'; $project->title_en = 'site'; $database->save($project); } \db\debug($solution); \db\debug($project); \db\debug($database->context->usage);
<?php include './000.config.php'; include '../db.php'; //create database link //first parameter of link stands for link name //in this case 'default' is link name $link = new \db\link('default', $config->database, $config->username, $config->password); $link->debug = true; //simple query fetch via link foreach ($link->query("show tables from mysql") as $row) { \db\debug($row); } //error fetch from link $result = $link->query("select nothing from void"); if ($link->error()) { \db\debug($link->error()); } //single value fetch from link $value = $link->value("select 1"); \db\debug($value); //single row fetch from link $row = $link->fetch("select 1,2,3,4"); \db\debug($row);