PHP quick tutorial

To get started with DSL platform and PHP, we suggest you try it out in an interactive environment at learn.dsl-platform.com. There you can write and execute your own PHP or browse the interactive examples. No registration is needed. Go ahead and try it out! Or, you can continue to browse this documentation...

Let's start with some simple DSL. Following DSL will generate a new PHP class Task that resides in Todo namespace and contains 3 properties: Name, IsDone and ID (autogenerated key).

module Todo {
    aggregate Task {
        string Name;
        bool IsDone;
    }
}

Create a new Task and store on the server using persist() function.

$newTask = new Todo\Task(array(
    'Name'   => 'DSL tutorial',
    'IsDone' => false
));
$newTask->persist();

Read a task from the server using its ID.

$task = Todo\Task::find($newTask->ID);

Update an existing task.

$task->IsDone = true;
$task->persist();

Delete all completed tasks.

foreach(Todo\Task::findAll() as $task)
    if($task->IsDone)
        $task->delete();

Default values Unspecified properties in constructor will assume default values. Following objects will contain same values.

$task1 = new Todo\Task();

$task2 = new Todo\Task(array(
    'Title'  => '',
    'IsDone' => false
));

Explicit key can be specified in brackets after aggregate name. This DSL defines aggregate Project with property "Code" as key:

module Company {
    aggregate Project(Code) {
        string Code;
        string Name;
    }
}

// find a project with specific code
$project = Company\Project::find('my-project-code');

If key is not explicitly specified, ID property is automatically created as autoincrementing key.

Reference a Project object inside Task.

aggregate Task {
    string Name;
    bool IsDone;
    Company.Project *Parent;
}

$myProject = new Company\Project(array('Name'=>'DSL platform'));
$myProject->persist();

// add task to project
$projectTask = new Todo\Task(array(
    'Name'   => 'P'
    'Parent' => $myProject
));
$projectTask->persist();

Simple arrays can be created using angle brackets.

aggregate Task {
    string Name;
    bool IsDone;
    string[] Tags;
}

$task = new Todo\Task(array(
    'Name' => 'Write tutorial',
    'Tags' => array('dsl', 'php', 'tutorial')
));

Search through records by writing a specification that satisfies specific conditions.

aggregate Task {
    string Name;
    bool IsDone;

    specification findDone 'it => it.IsDone == true';
}

$doneTasks = Todo\Task::findDone();

Search using arguments

aggregate Task {
    string Name;
    bool IsDone;

    specification findByName 'it => it.Name == search'
    {
        string search;
    }
}

$task = Todo\Task::findByName('DSL tutorial');