User Tools

Site Tools


t1.7_creating_a_data_structure_for_employees_and_departments_datarecord

Datarecords

The Datarecord class is the most central object in Platform and therefore it is imperative to understand it. It is used to create most or all data objects used in your application.

In this tutorial we will use it to build a Department object representing the departments in our company and a Employee object representing the employees working in the different departments.

There is a class called Templateclass which holds a template structure for creating new Datarecord objects. We'll be extending that.

The department

First we rename the class and overwrite the $database_table variable, which decides the name of the database table, that these objects are stored into.

department.php
class Department extends \Platform\Datarecord {
  protected static $database_table = 'departments';
  ...
}

Then we overwrite the buildStructure() function to build our desired data structure for a department.

department.php
  1. class Department extends \Platform\Datarecord {
  2. ...
  3. protected static buildStructure() {
  4. $structure = array(
  5. 'department_id' => array(
  6. 'invisible' => true,
  7. 'fieldtype' => self::FIELDTYPE_KEY
  8. ),
  9. 'title' => array(
  10. 'label' => 'Department title',
  11. 'required' => true,
  12. 'is_title' => true,
  13. 'fieldtype' => self::FIELDTYPE_TEXT
  14. )
  15. );
  16. self::addStructure($structure);
  17. parent::buildStructure();
  18. }
  19. }

This defines which fields we want in our object (and thereby our database). The structure is an array hashed by field names, which again refers to arrays defining the field with different keywords.

In line 5 we define we want a field called department_id. We define it should be invisible to the end user (line 6) and that the type should be the record key (line 7) which is required on each object.

Line 9 defines the next field called title, which we give a user-friendly name (line 10) and indicate that this field must be filled in every record (line 11) and that it contains the overall title of the record (line 12). Lastly we say that it should contain a text string (line 13).

That's it for the Department or almost… We need to go back to our Instance-object and add the following line to the initializeDatabase() function:

instance.php
  public function initializeDatabase() {
    parent::initializeDatabase();
    Department::ensureInDatabase();
  }

The employee

We create the employee object in a likewise manner:

employee.php
  1. class Employee extends \Platform\Datarecord {
  2. ...
  3. protected static $database_table = 'employees';
  4. ...
  5. protected static buildStructure() {
  6. $structure = array(
  7. 'employee_id' => array(
  8. 'invisible' => true,
  9. 'fieldtype' => self::FIELDTYPE_KEY
  10. ),
  11. 'full_name' => array(
  12. 'label' => 'Full name',
  13. 'required' => true,
  14. 'is_title' => true,
  15. 'fieldtype' => self::FIELDTYPE_TEXT
  16. ),
  17. 'department_ref' => array(
  18. 'label' => 'Department',
  19. 'fieldtype' => self::FIELDTYPE_REFERENCE_SINGLE,
  20. 'foreign_class' => 'People\\Department'
  21. );
  22. self::addStructure($structure);
  23. parent::buildStructure();
  24. }
  25. }

This is exactly the same, with only line 17-20 requiring further explanation. This field type is a reference to another datarecord object, where we name our just created Department class. In other words we use this field to link the employee to the department where they work.

Lastly we also add this class to the instance:

instance.php
  public function initializeDatabase() {
    parent::initializeDatabase();
    Department::ensureInDatabase();
    Employee::ensureInDatabase();
  }

And if we now call this code:

  1. $instance_id = Platform\Instance::getActiveInstanceID();
  2. $instance = new People\Instance();
  3. $instance->loadForRead($instance_id);
  4. $instance->initializeDatabase();

We will see that two new table suddenly appears in the instance database, ready to store data from our two datarecord objects. If you at some point wants to adjust the object definitions, then you can just adjust them and call the function above again. You will then see that the database automatically adjusts to your new definition, while trying to keep records already stored in it.

This was a little long, but now that's it.

t1.7_creating_a_data_structure_for_employees_and_departments_datarecord.txt · Last modified: 2021/03/10 20:47 by sahl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki