Skip to content

API Design

Romans Malinovskis edited this page Apr 17, 2016 · 3 revisions

(Request For Comments)

This page consists of several "use cases" showing how our object interface would work. Please feel free to comment and suggest improvements.

Defining DataSet / Model

Model represents a DataSet. It describes a "category of entities" that have some business-logic similarities. The class definition (init method) is declarative by nature.

class User extends dataset\Model {
    protected $table = 'user';
    protected function init() {
        parent::init();

        $this->addField('name');
        $this->addField('email');
        $this->addField('is_expired')->type('boolean');

        $this->hasMany('Order');
    }

    function getExpried() {
        return $this->addCondition('is_expired', true);
    }
}

The above example describes the following:

  • $table property is a default "table" or "collection" inside data engine and shall be used by persistence layer unless we define it differently.
  • addField() register a new field as a part of a model. Each time a new object is returned that describes field. We can further set type, validation, caption and other field for each field.
  • hasMany() describes relation to a different DataSet. In this case we would be able to use $user->ref('Order') to get set of order for a particular user.
  • getExpired() is our custom method, which also is designed to give us a different DataSet that is a sub-set to User.