Louder Developer Zone
Articles, Tutorials and Tips from the community and Louder developers

Use Firebird/Interbase with Kumbia Enterprise

Publish at Thursday, April 01, 2010
By Core Developers - Louder

Firebird is a relational database management system (RDBM) open sourced by Borland. Firebird with PostgreSQL and MySQL is one of the best-known open source databases.

Requirements

Kumbia Enterprise uses php extensions "interbase" and "pdo_firebird" to connect to Firebird / Interbase using the native layer, so it is necessary that any of these 2 extensions are available.

Configure Connection to Firebird / Interbase

You can make connections to Firebird / Interbase databases using TCP/IP or IPX protocols. The databases can be deployed on the same server or as a remote resource. To configure the database in environment.ini use the following configuration:


//Using php_interbase extension
[development]
database.type = firebird
database.host = localhost
database.username = sysdba
database.password = masterkey
database.name = "/home/antonio/company.fdb"

//Using php_pdo_firebird extension
[development]
database.layer = pdo
database.type = firebird
database.username = sysdba
database.password = masterkey
database.dsn = "dbname=localhost:/home/antonio/company.fdb"

Connecting to Firebird / Interbase

If you make a direct connection can use DbLoader::factory as follows:


$db = DbLoader::factory("Firebird", array(
	"host" => "localhost",
	"username" => "sysdba",
	"password" => "masterkey",
	"name" => "/home/antonio/company.fdb"
));

Interacting with the Database

Firebird adapter has been implemented throughout the Db component API, so naturally you can use all its functionality:


//Do a Query
$db->query("SELECT * FROM customers");
$numberRows = $db->numRows();

//If there are rows then show it
if($numberRows>0){
	while($customer = $db->fetchArray()){
		echo $customer[0], "\n";
		echo $customer["name"], "\n";
	}
}

//Show the second customer
$db->dataSeek(1);
$row = $db->fetchArray();
echo $row["name"], "\n";

//"customers" table exists?
$db->tableExists("customers");

//List all the database tables
$tables = $db->listTables();
foreach($tables as $table){
	echo $table, "\n";
}

//Query all table meta-data
$fields = $db->describeTable("customers");
foreach($fields as $field){
	print_r($field);
}

LIMIT Implementation

As with the other adapters Db, Firebird adapter has been implemented the SQL language extension called LIMIT that allows to define the number of records returned by the database in a single query:


//Do a Query
$sql = $db->limit("SELECT * FROM customers", 10);
$cursor = $db->query($sql);
$numberRows = $db->numRows($cursor); // 10

Using Firebird with ActiveRecord

All ActiveRecord

Firebird/Interbase do not support identity columns (auto numeric), instead it is possible to use generators. A generator can be created using the usual convention for sequences TABLE_FIELD_SEQ. If the primary key field called ID as well then the model can define as follows:


class Customers extends ActiveRecord {

}


If no primary key field called ID then we can define the generator as follows:

class Customers extends ActiveRecord { public function initialize(){ $this->setIdGenerator("Native", "code"); } }

If the generator has a different name to the convention you can use a native generator to indicate your name:


class Customers extends ActiveRecord {

	public function sequenceName(){
		return "CUSTOMERS_CODE_GEN";
	}

	public function initialize(){
		$this->setIdGenerator("Native", "code");
	}

}

Then use the model as usual:


//Create a customer
$customer = new Customers();
$customer->name = "John Applesed";
$customer->created = new DbRawValue("CURRENT_TIMESTAMP"); #Set the database system date
$customer->save();

//Show the ID that was assigned to record from the generator (sequence)
echo $customer->id;

//Show the name of all customers
foreach(Customers::findAll() as $customer){
	echo $customer->name, "\n";
}

Conclusions

  • Firebird is a powerful open source engine which can be exploited in Kumbia Enterprise
  • The complete functionality of Db and ActiveRecord components can be utilized with Firebird/Interbase databases

If you have problems using the Firebird/Interbase adapter, you can get support from the framework developers in our discussion group on google groups.

Tell friends about this article on social networks:



blog comments powered by Disqus

Previous: Introducción a KEQL. Parte 1 Next: Geo-Localización en Kumbia Enterprise

Colaborate

Colaborate

We invite you to submit articles and tutorials to the Developer Zone.

Archive

  • Mayo 2009

Maybe you are interested

Added value to your Business.

Become a Solution Partner Louder Now.

Bring to the Open-Source retroactively..

Learn more about Shared Louder Labs