Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Docs Database Connection

Frank Kleine edited this page Apr 7, 2012 · 1 revision

RDBMS and IoC

Like other parts of Stubbles the RDBMS features IoC support as well. To get a rdbms connection in your application class you just need to get it injected.

Applies to Stubbles 1.0.x: To enable this simply add the binding module net::stubbles::rdbms::ioc::stubDatabaseBindingModule. This will add bindings for database connections as configured.

Applies to versions starting with Stubbles 1.1.0: no special binding module is required, only a configuration in the ini format as described on database configuration.

A code example

All you have to do is simply tell the Stubbles IoC mechanism you need a database connection:

<?php
class MyAppClass
{
    protected $connection;

    /**
***constructor
     *
***@param  stubDatabaseConnection  $connection
***@Inject
***@Named('connection_id')
***/
    public function __construct(stubDatabaseConnection $connection)
    {
        $this->connection = $connection;
    }

    public function doSomething()
    {
        // do some work with connection
    }
}
?>

Here we let the IoC container inject the database connection. By supplying the @Named annotation we can say which connection should be supplied - it should be the same Id as used in the configuration. If no @Named annotation is supplied it will fallback to the connection with the Id default.

Options for the binding module

Only applies to Stubbles 1.0.x.

The binding module has two optional options for the constructor:

  • fallback, defaults to true: if enabled the connection provider will fall back to return the default connection in case the requested connection does not exist. This should not be confused with the fallback if no @Named annotation is provided. The latter fallback is always be available, regardless of this switch.
  • descriptor, defaults to rdbms: this can be used to use different configuration files. By default the configuration file projects/$PROJECT/config/xml/rdbms.xml will be used.
Clone this wiki locally