Table of Contents
Web:Extend provides an unified interface to various databases, including MySQL, PostgreSQL, SQLite and Oracle. The unified interface allows you to access any of these database using the same PHP code, meaning you only have to learn it once in order to use it everywhere.
Note however that the database access layer is not an ORM. Web:Extend lets you access the database directly and does not prevent you to use functionality that only exists in one of the DBMS. You are entirely free to use PostgreSQL's arrays or MySQL's sets. This means you can freely use the full power of your DBMS.
The database access layer provides an easy-to-use interface to your database.
There is only four types of objects: the database object, the result set object, the row object and the prepared statement object.
Both the database and the prepared statement objects are used to query the database,
while the result set and the row objects are used to handle the results of SELECT queries.
Handling results is really easy: the result set is an iterator, meaning you can use foreach directly on the object, like if it was an array of rows.
Similarly, the row object acts like an array by implementing ArrayAccess.
Note however that by default the rows are returned as arrays.
You can connect to a database either manually or using the application module. This chapter will explain both ways.
You only need to modify the configuration file in order to connect to a database. You only need to fill the connection settings that will be used to connect to the database when you first try to access it. Note that the framework will never connect to the database until you need it. If your script use the application module but does not access the database, it will not try to connect.
The parameters can vary depending on the database driver. This chapter will explain how to connect to a PostgreSQL database. Examples may vary depending on the DBMS used.
There is 5 parameters: the database driver used, the hostname, login name, password, and database name.
The last 4 are common when connecting to a database.
The first parameter is the name of the class used to access the database,
and can be either of weePgSQLDatabase for PostgreSQL,
weeMySQLDatabase or weeMySQLiDatabase for MySQL,
or any other driver available.
You need to modify the following parameters in the configuration file:
# Database settings
db.driver = weePgSQLDatabase
db.host = localhost
db.user = username
db.password = password
db.name = dbname
Once the application is started you can access your database using weeApp()->db
and start writing queries.
To connect without the application module you need to create the database driver yourself.
It takes an associative array containing the settings as a parameter.
The parameters are the same 4 we already discussed, except their names do not include the db. part.
This is demonstrated by the following example, where we connect to a PostgreSQL database:
<?php $aDbSettings = array( 'host' => 'localhost', 'user' => 'myusername', 'password' => 'mypassword', 'dbname' => 'mydb', ); $oDb = new weePgSQLDatabase($aDbSettings);
You can then use the $oDb object to perform queries on your database.
Please note that some drivers may not require all these settings defined, or require totally different settings. Please refer to the documentation for your database driver before trying to connect. There might also be optional, driver-specific settings that are documented in the drivers documentation.
You usually shouldn't need to close the connection, since PHP will close it at the end of the script's execution. But you might consider closing it yourself, if you are running a time-intensive or memory-intensive script and you do not need to have a connection open the whole time, to free up resources. You can do it by simply using unset on the database object, as in:
<?php
unset($oDb);
This will effectively destroy the database object and close the connection.
Note however that it is not possible to close the connection manually at this time using the application module.