PhpToolCase
Api Documentation Version 0.9.2
|
The database connection manager is an excelent tool that can help you to manage database connections for your application / website.
This component will store the connection details and conveniently initialize a pdo object to handle queries.
This tool offers also full support for the PtcQueryBuilder component, if specified as an option when adding a connection.
Main Features:
To start managing your database connections with the component, we can use the PtcDb::add( ) method to add a connection:
We just added a new connection to the connection manager.
"$name"
is not specified, the class will automatically use the default connection.All possible connection options that can be passed to the PtcDb::add( ) method, are stored in the PtcDb::$_connectionOptions property:
The following sections will describe these options in details:
This option sets the connection name.
Default value: default
(string)
"$name"
argument to the PtcDb::add( ) method directly.This is the driver that the pdo object will use, currently only mysql is supported.
Default value: mysql
(string)
This option sets the database username.
Default value: root
(string)
This option sets the username password.
Default value: default
(string)
This option sets the databse host.
Default value: localhost
(string)
This option sets the database name.
Default value: database
(string)
This option sets the charset for the database connection.
Default value: utf8
(string)
This option adds query builder support to the connection manager, keep in mind that the PtcQueryBuilder component needs to be present.
See Using the Query Builder for a detailed explanation on using the Query Builder.
Default value: false
(bool)
This option sets the name of the query builder component, usefull if the component has been extended or is under a namespace.
true
.
Default value: PtcQueryBuilder
(string)
This option sets attributes to the pdo object.
These are the default attributes that are been set when the PtcDb::add( ) method is called:
To use the Pdo object to handle queries with the default connection is very simple, here is and example that speaks for it self:
To use another connection you can use the PtcDb::getPdo( ) method:
To start using the PtcQueryBuilder component with the database connection manager we need to specify it when we are adding a connection.
Here is a practical example:
To call methods from the PtcQueryBuilder is just like using the Pdo, here is an example using the default connection:
To retrieve the query builder object from another connection, the method PtcDb::getQB( ) can be used:
Refer to the Query Builder Component (PtcQueryBuilder) user manual for detailed usage.
To retrieve the options from a previously configured connection with the PtcDb::add( ) method, we can use the PtcDb::getConnection( ) method:
$name
is not specified, all configured connections will be returned.