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.
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
Code
/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:
APF-Konfiguration
[sectionname]
DB.Host = ""
DB.User = ""
DB.Pass = ""
DB.Name = ""
[DB.DebugMode = "true|false"]
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.
The debug mode can be used to display the executed statement directly on screen. If you like to
monitor the statements executes, you may use the optional parameter
$logStatement applied
to the
executeStatement() and
executeTextStatement() method. If set to
true, the currently executed statement is appended to a log file. Details on the function
can be obtained from the
API documentation.
To be able to use the
connectionManager the component must be imported by
PHP-Code
import('core::database','connectionManager');
prior to use. Further, you can create a database driver instance using the following code snippet:
PHP-Code
$cM = &$this->__getServiceObject('core::database','connectionManager');
$db_driver = &$cM->getConnection('sectionname');
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.
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!
In order to create an instance of the MySQL abstraction class the following configuration has to
be included in the config file mentioned above:
APF-Konfiguration
[MySQL]
DB.Host = "host"
DB.User = "user"
DB.Pass = "pass"
DB.Name = "name"
DB.Type = "MySQLx"
[DB.DebugMode = "true|false"]
[DB.Charset = ""]
[DB.Collation = ""]
The two parameters
DB.Charset and
DB.Collation are used to
configure the
character set and the
collation of the MySQL connection. The
directive
DB.Charset sets the MySQL variables
- character_set_client
- character_set_connection
- character_set_results
and the value of
DB.Collation is applied to the variables
- collation_connection
- collation_database
Both params are optional and can be defined alternately. There is no need to specify both params at
the same time.
Afterwards, the instance of the abstraction layer can be created using the following lines of code:
PHP-Code
$cM = &$this->__getServiceObject('core::database','connectionManager');
$MySQL = &$cM->getConnection('MySQL');
Due to performance issues, the connection manager only creates singleton drivers.
Any SQLite abstraction layer instance can be configured by
APF-Konfiguration
[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:
PHP-Code
$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.