{ /** * 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
{ public $id; /** * locale */ public $name; public function __construct($name_en, $name_fr) { $this->name_en = $name_en; $this->name_fr = $name_fr; } } $database = new \db\database('db_samples', new \db\link('default', $config->database, $config->username, $config->password)); $database->link('default')->debug = true; $database->locales(array(new \db\locale('en'), new \db\locale('fr'))); $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) {
* @var client */ public $client; /** * @var string */ public $worker; } class client { public $id; 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('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;
} } //take a look how user.group relates to group by adding in public $group comment directive '@var group' class group { public $id; public $name; public function __construct($id, $name) { $this->id = $id; $this->name = $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->add('group'); $database->update(); $administrator = $database->group->load(1); if (!$administrator) { $administrator = new group(null, 'Administrator'); $database->group->save($administrator); } \db\debug($administrator); $john = new user(1, 'John'); //here the relation happend ! $john->group = $administrator; //here if save is not happening //it means we already have user with id 1 if (!$database->user->save($john, \db\query::insert)) { //than update it to save group
$this->name = $name; } } //take a note we specified in public $girl comment 'enum' and also master class 'girl' as @var class girl { public $id; public $name; public function __construct($name) { $this->name = $name; } } $database = new \db\database('db_samples', new \db\link('default', $config->database, $config->username, $config->password)); $database->link('default')->debug = true; $database->add('boy'); $database->add('girl'); $database->update(); $john = new boy('John'); //\db\debug ($database->boy); $database->boy->save($john); //lets see if john is single \db\debug($john); $girls = array(new girl('Anne'), new girl('Fiona'), new girl('Nino')); //by the way this is multi save example too $database->girl->save($girls); $john->girl[] = $girls[0]; $john->girl[] = $girls[2]; //well john is single //let us create some girls $database->boy->save($john);
} /** * cache long */ class part { public $id; public $name; public function __construct($name) { $this->name = $name; } } $database = new \db\database('db_samples', new \db\link('default', $config->database, $config->username, $config->password)); $database->link('default')->debug = true; $database->add('page'); $database->add('part'); $database->update(); $parts = $database->part->load(); if (!$parts) { $parts = array(new part('register'), new part('profile'), new part('menu'), new part('password'), new part('news'), new part('gallery')); $database->part->save($parts); } $pages = $database->page->load(); if (!$pages) { $database->page->save(new page('home', array($parts[0], $parts[3], $parts[2]))); $database->page->save(new page('account', array($parts[1], $parts[4], $parts[5]))); $database->page->save(new page('service', array($parts[3], $parts[2], $parts[1]))); $database->page->save(new page('about', array($parts[2], $parts[4], $parts[0]))); } //after first populate session this code will never run select queries