First we will create simple page named "dbc_test" that will be used to illustrate usage of DBC classes. If you don't recall how to do it please see the section called “Creating simple page” and the section called “Page view's TPL files "classic" structure”. You will need to create following files:
In order examples to work you will have to create table named users in your DB with columns id (unsigned int), username and password (both varchar). Also you will have to create table users_seq of type InnoDB with one column id (unsigned int) which will be used to emulate sequence. Insert one row with value 0.
Sites created with Tangra framework almost always use MySQL tables of type InnoDB because of the support for transactions. If you are not familiar with transactions may be this article will be helpful: » Database transaction
Initially files contents should look like this:
htdocs/dbc_test.php
<?php
require_once('boot.inc.php');
boot('my_site');
require_once($WSC->get_site_inc_dir().'pages/dbc_test_wp.class.php');
session_start();
$page = new DBC_Test_WP('dbc_test');
$SITE->run($page, new Web_Context);
hidden/inc/pages/dbc_test_wp.class.php
<?php
// $Id$
require_once($WSC->get_site_inc_dir().'site_web_page.class.php');
class DBC_Test_WP extends Site_Web_Page {
public function init() {
parent::init();
$v = new Site_Web_Page_View($this);
$this->add_view($v);
}
public function run() {
$view = $this->get_view('default');
return $view;
}
}
hidden/tpl/pages/dbc_test/dbc_test_title_en.tpl
DBC Test
hidden/tpl/pages/dbc_test/dbc_test_default_en.tpl - leave it empty for now.
Point your browser to tft.myhost/dbc_test.php and you will see just the title "DBC Test" - it is OK for now.
Before starting to work with any DBC class you have to include it in your WP. Add
require_once($WSC->get_site_inc_dir().'classes/user/user_dbc.class.php');
Here is how we set username and password fields and save the record in run() method:
public function run() {
$user = new User_DBC(); // instantiating class User
$user->set_username('someusername'); // setting username
$user->set_password('somepassword'); // setting password
$dbc = $this->get_dbc(); //getting connected DB_Connection
try {
$dbc->start_trans(); // starting transaction
$user->save($dbc); // saving the record
$dbc->complete_trans(); // finishing the transaction
} catch (Exception $e) { // if something wrong happens above exception will be thrown so we catching it in order to do clean up
$dbc->fail_trans(); // explicitly failing the transaction
$dbc->complete_trans(); // finishing the transaction
throw $e; // rethrowing the exception
}
$view = $this->get_view('default');
return $view;
}
Check your database table users and you should find your newly created record.
Tangra framework uses transactions constantly when performing data modification operations. You are advised to use them too - that way will you ensure that there will be no inconsistencies in your database.
For convenience when using transactions you may add following code fragment to your editor's code templates:
try {
$dbc->start_trans();
$dbc->complete_trans();
} catch (Exception $e) {
$dbc->fail_trans();
$dbc->complete_trans();
throw $e;
}
Loading record is usually done by its ID. Lets load the record that we saved above:
public function run() {
$user = new User_DBC(); // instantiating class User
$dbc = $this->get_dbc();
if ($user->load_by_id($dbc, 1)) { // trying to load record with 1
$this->export('user', $user); // exporting loaded record to tple
}
$view = $this->get_view('default');
return $view;
}
Note: ID of above example may differ in your database (i.e. different than 1)
public function run() {
$user = new User_DBC(); // instantiating class User
$dbc = $this->get_dbc();
if ($user->load_by_id($dbc, 1)) { // trying to load record with 1
$user->set_password('newpassword'); // setting new password
try {
$dbc->start_trans();
$user->save($dbc); // saving (updating) the record
$dbc->complete_trans();
} catch (Exception $e) {
$dbc->fail_trans();
$dbc->complete_trans();
throw $e;
}
}
$view = $this->get_view('default');
return $view;
}