Skip to main content

Database

Database connections are available from the Service container.

To get the current site's database connection from the service container:

$db = Jadu_Service_Container::getInstance()->getSiteDB();

To get the main site's database connection, even when executing code on a Galaxies site:

$mainDb = Jadu_Service_Container::getInstance()->getMainDB();

To get the database connection for a named site:

$siteDb = Jadu_Service_Container::getInstance()->getConnectionForSiteURL('');

DataMapper classes

DataMapper classes in Jadu Central provide a mapping between stored data (in a MySQL or MSSQL database) and objects. Use of DataMappers allows better separation of business logic and data access. This allows code to be unit tested more easily and results in a cleaner application class structure.

DataMappers will exist in a one-to-one mapping with an object.

Usage

In order to achieve this separation, the script must now instantiate any required DataMappers to retrieve objects.

Not all areas of Jadu currently use the DataMapper model. Refer to individual feature documentation for details on whether or not a DataMapper is available.

Example: Fetch and update a Language Pack

$jadu = Jadu_Service_Container::getInstance();
$packMapper = new Jadu_Language_DataMapper_Pack($jadu->getSiteDB(), $jadu->getCacheManager());

$languagePack = $packMapper->getByID($id);
$languagePack->title = "New Title";

$error_array = $languagePack->validate();

if (count($error_array) == 0) {
$packMapper->save($languagePack);
}

Abstract Class

An abstract class Jadu\Abstracts\DataMapper has been created that all DataMappers should extend.

This class has a public method that takes parameters and builds a query based on them.

getQuery(array $params)

This method will return a query based on the parameters passed in. It does not build the query itself but passes the parameters to the appropiate methods to build each section of the query.

Example:

$params = [
'select' => ['name', 'value'],
'alias' => 'questions'
'join' => array(
new Join('INNER JOIN', 'JaduAnswers', array(
array('answers.question', 'questions.id', '=')
), 'answers')
)
'where' => [
'name' => ['%town%', 'LIKE']
],
'orderBy' => [
['value', 'ASC']
]
];

Passing the above parameters in would result in the following query

SELECT name, value 
FROM JaduQuestions AS questions
INNER JOIN JaduAnswers as answers ON answers.question = questions.id
WHERE name LIKE '%town%'
ORDER BY value ASC

Implementation

All DataMappers should extend the base class Jadu\Abstracts\DataMapper. As a minimum, each DataMapper will need to implement the following methods:

getTableName()

This is used to build SQL statements and as a Cache namespace. It should simply return the name of the table that the DataMapper will act upon.

getTableColumns()

This is used to build SQL statements. It should simply return an array of the columns names in the table that the DataMapper will act upon.

getRequiredColumns()

This is used to ensure any required columns within a query are selected. It should simply return an array of column names that are required.

transformRow(array $row)

This method is used to convert an array containing a single row from an SQL result set into an object. Usually, this method will do the following:

  • instantiate a new instance of the object
  • set properties on that object with values from $row
  • return the object

transformEntity($entity)

This method performs the reverse operation of transformRow(). It is given an object of the type it is responsible for mapping and creates an array to be used with an SQL insert or update statement.