Quicknavi |
|
«Previous page | Home | Next page »
Class reference table - connectionManager
The notion of the connectionManager is to define the configuration and implementation of a database
abstraction and access layer. Ideally, this approach eases switching from one database driver to
another.
Despite the fact, that this procedure is described a little bit too idealistic - different databases
often have different features - the connectionManager itself gives a common understanding of how an
application's data layer should communicate with the database abstraction layer.
1. Configuration
The connectionManager is a kind of factory to concrete implementations of database
driver classes. To load the desired driver the abstraction layer must be configured using the config
file
/apps/config/core/database/{CONTEXT}/{ENVIRONMENT}_connections.ini
Details on configuration files can be found in the
configuration chapter. The configuration
file mentioned above must contain one section for each driver layer:
[sectionsname]
DB.Host = ""
DB.User = ""
DB.Pass = ""
DB.Name = ""
DB.Type = ""
DB.DebugMode = ""
The sectionsname is used to create the driver instance, DB.Host
contains the hostname of the database server, DB.User and DB.Pass
define the logon credentials and DB.Name defines the name of the database to use.
DB.Type is aimed to switch the debug mode on of off.
2. Practice
To be able to use the connectionManager the component must be imported by
import('core::database','connectionManager');
prior to use. Further, you can create a database driver instance using the following code snippet:
$cM = &$this->__getServiceObject('core::database','connectionManager'); $db_driver = &$cM->getConnection('db_driver');
3. Extending the database drivers
The class AbstractDatabaseHandler describes the interface of a database driver that
can be created using the connectionManager. In order to implement another
abstraction layer any other driver must inherit from AbstractDatabaseHandler and
reside in the core::database namespace.
4. Existing abstraction layers
The adventure php framework comes with two different abstraction layers:
- MySQLx: MySQL driver. Based on the mysql_* functions.
- SQLite: SQLite driver. Based on the sqlite_* functions. Needs PHP > 5.0.0!
4.1. MySQL driver
In order to create an instance of the MySQL abstraction class the following configuration has to
be included in the config file mentioned above:
[MySQL]
DB.Host = "host"
DB.User = "user"
DB.Pass = "pass"
DB.Name = "name"
DB.Type = "MySQLx"
DB.DebugMode = "true|false"
Afterwards, the instance of the abstraction layer can be created using the following lines of code:
$cM = &$this->__getServiceObject('core::database','connectionManager'); $MySQL = &$cM->getConnection('MySQL');
Due to performance issues, the connection manager only creates singleton drivers.
4.2. SQLite driver
Any SQLite abstraction layer instance can be configured by
[SQLite]
DB.Name = "/path/to/my/database.sqlite"
DB.Type = "SQLite"
DB.DebugMode = "true|false"
Please note, that host, user and password don't have to be configured, due to the fact, that SQLite
is an integrated database engine. Creating an instance of the driver class looks like this:
$cM = &$this->__getServiceObject('core::database','connectionManager'); $SQLite = &$cM->getConnection('SQLite');
Comments
Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.
|