9.2. Setting Up

The system requires little set-up. Let’s declare three classes.

<?php
class Person extends Xyster_Orm_Entity
{
}
class PersonSet extends Xyster_Orm_Set
{
}

Those are the entity and the entity set. Below is the mapper class. Since the mapper will connect to a SQL database, we can just extend from Xyster_Orm_Mapper. To see how to set up your database connection, see the Xyster_Orm_Mapper section below.

<?php
class PersonMapper extends Xyster_Orm_Mapper
{
    protected $_domain = 'myDb'; // the 'domain' associated with the back-end
}

And that's it. Everything else about this entity is determined automagically: table, primary key, and columns. Let’s also specify a cache lifetime and an index.

<?php
class PersonMapper extends Xyster_Orm_Mapper
{
    protected $_domain = 'myDb';
    protected $_table = 'person';
    protected $_lifetime = 90;
    protected $_index = array('name_index' => array('name'));
}

The table is by default the "underscore case" of the entity class name, so "ExampleTable" would be "example_table". The primary key is looked up from the table metadata in the data store. The column names are the camel case of the fields in the data store. They are looked up for the table and cached. For example, if "example_table" had "column_one", "column_two", "and_column_three", the columns array should contain "columnOne", "columnTwo", "andColumnThree". The lifetime property should be a number in seconds to cache entities in the secondary cache (see the Secondary Cache heading under Xyster_Orm). The index is used to cache entities by a key/value pair. In our example, we expect the "name" to be unique.

If for some reason you need to change how a mapper translates column names, or gets an entity set, these and other methods can be overwritten. See the Xyster_Orm_Mapper API for more information.