Version 2, last updated by partikule at October 13, 2010 09:29 UTC

ORM Tools mapper basics (deprecated)

Creating a class to insert into the database

The class

The class is just a normal PHP class, nothing more. There are a few restrictions, however:

  • The constructor cannot take any parameters (will fix it in a later revision, where a factory can be specified).
  • The properties storing the data must be public

Otherwise you can treat it as any PHP class: add methods, a constructor, accessory methods, etc.

Telling the mapper to use it

To define the class as a mapped class, you create a PHPdoc block for it, and inside this you add the @ot tag:

/**
 * @ot
 */
class User
// ...

This is not enough, however, as the mapper requires the user to define at least one property to be mapped to the database. The properties are defined in the exact same way, with the @ot tag in the PHPdoc:

/**
 * @ot
 */
public $id;

The @ot tag can take a few parameters, the important ones for basic usage are these:

  • id - makes the property a primary key, at least one is required per class (if you want to set these manually, use id manual)
  • type - the sql type the property uses, makes ORM Tools perform the correct typecasts
  • name - to remap the property to link to a column with another name (by default it links to the same name as the property's name)

Specifying relations

To specify relations, you create a property which will contain the related object(s)—then you add the relation defining code in the PHPdoc:

/**
 * @ot relates to_many post objects
 */
public $posts = array();

There are three relation types:

  • owned_by - Specifies that this class is owned by the other, the foreign key is in this table, linking to the primary key in the related table
  • to_one - Specifies that this class relates to one other object, the foreign key in the related object linking to the primary key
  • to_many - as to_one, but allows multiple related objects

Usage:

@ot relates [to_one|to_many] <related_class> (objects)
@ot owned_by <related_class>

Using the mapper

Use the methods on the static class Ot (aliased as Db):

$u = new User();
$u->name = 'Foo';
Ot::save($u);

Methods:

  • find
  • save
  • delete
  • related

Check their PHPdoc comments on usage

Example

Classes

/**
 * @ot
 **/
class User
{
    /**
     * @ot type unsigned int
     * @ot id
     */
    public $id;

    /**
     * @ot type varchar(60)
     */
    public $name;

    /**
     * @ot relates to_many post objects
     */
    public $posts = array();
}

/**
 * @ot
 */
class Post
{
    /**
     * @ot type unsigned int
     * @ot id
     */
    public $id;

    /**
     * @ot type varchar(45)
     */
    public $title;

    /**
     * @ot owned_by user
     */
    public $user = null;
}

Code

// get user with id 2, and post with id 2
$user = Db::find('user', 2);
$post = Db::find('post', 2);

// dump the objects
Db::print_r($user);
Db::print_r($post);

// assign the post, creating a relation with Db::save()
$user->posts[] = $post;
Db::print_r($user);
Db::save($user);

// get a user with related posts (the one we loaded earlier
$u = Db::find('user')->where('user.id', 2)->related('posts')->get_one();

// now unset the relations we created
$u->posts = array();
Db::save($u);