Chapter 4. Configuration

The configuration consists of an associative array with the elements:

connection

Database connection settings

classes

Mapping configuration for each of the entity classes

useGettersAndSetters

Optional. Whether to use getters and setters instead of the properties directly. This can be overridden by the entity config. Defaults to false.

This array is generally defined in a php script such as outlet-config.php. Outlet is then initialized with configuration array as the parameter:

Outlet::init(include APPROOT.'/outlet-config.php');

Here is a sample configuration:

Example 4.1. Sample configuration: outlet-config.php

return array(
  'connection' => array(
    'dsn'      => 'mysql:host=myserver.com;dbname=mydb',
    'username' => 'user',
    'password' => 'pass',
    'dialect' => 'mysql'
  ),
  'classes' => array(
    'Project' => array(
      'table' => 'projects',
      'props' => array(
        'ID'        => array('id', 'int', array('pk'=>true, 'autoIncrement'=>true)),
        'Name'      => array('name', 'varchar')
      ),
      'associations' => array(
        array('one-to-many', 'Bug', array('key'=>'ProjectID'))
      )
    ),
    'Bug' => array(
      'table' => 'bugs',
      'props' => array(
        'ID'        => array('id', 'int', array('pk'=>true, 'autoIncrement'=>true)),
        'Title'     => array('title', 'varchar'),
        'ProjectID' => array('project_id', 'int'),
        'Description' => array('description', 'varchar')
      ),
      'associations' => array(
        array('many-to-one', 'Project', array('key'=>'ProjectID'))
      )
    )
  )
);

Database Connection

array(
  'dsn' => 'mysql:host=myhost.com;dbname=testdb',
  'username' => 'testdbuser',
  'password' => 'testdbpass',
  'dialect' => 'mysql'
)
dsn

A PDO connection string

username

A username if the database driver requires it

password

A password if the database driver requires it

dialect

The dialect to use. It can be 'mysql', 'mssql', 'pgsql' or 'sqlite'.

Entity Mappings

array(
  'User' => array(
    'table' => 'users',           // name of the database table
    'props' => array(...),        // properties mappings
    'associations' => array(...)  // relationships mappings
  ),
  'Project' =>
  ...
)

An array of class mappings indexed by the name of the entity class. Each class mapping is an associative array with the elements:

table

Name of the database table or view that this entity maps to

props

An array of property mappings

associations

Optional. An array of association mappings

plural

Optional. The noun to use when referring to more than one. This is the name used by default on association methods if there's no 'plural' setting set on the association itself. Defaults to the entity name plus an 's'.

useGettersAndSetters

Optional. Whether to use getters and setters instead of the properties directly. When set, this will override the global configuration of the same name. Defaults to the global configuration, which in turn defaults to false.

sequenceName

Optional, specify it if you are using PostgreSQL. Since PostgreSQL uses sequences instead of auto increment columns, the PDO driver need the sequence name in order to get the generated new id. If not specified it will use the default: {table_name}_{column_name}_seq

Property Mappings

array(
  'ID'        => array('user_id', 'int', array('pk'=>true, 'autoIncrement'=>true)),
  'FirstName' => array('first_name', 'varchar'),
  ...
)
  • The indexes are the properties of the class being mapped.

  • The first element of each mapping is the name of the column.

  • The second is the column type, which can be: int, varchar, datetime or float.

  • The third element is an array of optional settings:

    pk

    Whether this field is (part of) the primary key. Defaults to false.

    autoIncrement

    Whether this field is automatically incremented by the database. Defaults to false.

    default

    A default value. It can be an integer or a string.

    defaultExpr

    A default sql expression or function, such as NOW().

Relationship Mappings

array(
  array('one-to-many', 'Bug', array('key'=>'BugID')),
  array('many-to-one', 'Project', array('key'=>'ProjectID')),
  ...
)
  • The first element of an association mapping is the type, it can be either one-to-many or many-to-one.

  • The second element is the entity class that this relation refers to (related entity).

  • The third element is a set of options which differ depending on the type of association.

    one-to-many
    key

    The name of the property on the related entity that contains the primary key of this entity.

    name

    Optional. Defaults to the name of the related entity.

    The name to give the relationship. If this association defines the relationship between a bug and the people who are assigned to it (one-to-many from Bug to User), and you set the name to 'Assignee', the method names on the bug object will be 'getAssignees', 'setAssignees', and 'addAssignee'.

    plural

    Optional. The noun to use when creating methods that return more than one entity. Only necessary for associations that use something other than the entity name (such as 'Sender' or 'Creator' for a an association with a 'User'). Defaults to the association name plus an 's'.

    many-to-one
    key

    The name of the property on this entity that contains the primary key of the related entity.

    name

    Optional. Defaults to the name of the related entity.

    The name to give the relationship. If this association defines the relationship between a bug and the user who reported it (many-to-one from Bug to User), and you set the name to 'Reporter', the method names on the bug object will be 'getReporter' and 'setReporter'.

    optional

    Optional. Defaults to false.

    Whether to allow this relationship to be optional (allow null).

    one-to-one
    key

    The name of the property on this entity that contains the primary key of the related entity.

    name

    Optional. Defaults to the name of the related entity.

    The name to give the relationship. This value is used to create the method that returns the associated entity.

    optional

    Optional. Defaults to false.

    Whether to allow this relationship to be optional (allow null).

    many-to-many
    table

    The name of the linking table.

    tableKeyLocal

    The name of the column on the linking table that contains the primary key value of this entity.

    tableKeyForeign

    The name of the column on the linking table that contains the primary key value of the foreign entity.

    plural

    Optional. The noun to use when creating methods that return more than one entity. Only necessary for associations that use something other than the entity name (such as 'Sender' or 'Creator' for a an association with a 'User'). Defaults to the association name plus an 's'.