Day 3: The Data Model

Those of you itching to open your text editor and lay down some PHP will be happy to know today will get us into some development. We will define the Jobeet data model and use an ORM to interact with the database.

The Relational Model

The user stories we saw yesterday describe the main objects of our project: jobs, affiliates, and categories. Here is the corresponding entity relationship diagram:

In addition to the columns described in the stories, we have also added a created_dts, updated_dts and deleted field to some tables. created_dts will be set to the value to the current system time when a record is created. We will do the same for updated_dts fields: Their value will be set to the system time whenever the record is updated.

The Models

To store the jobs, affiliates, and categories, we obviously need a relational database. But as Agile Toolkit is an Object-Oriented framework, we like to manipulate objects whenever we can. For example, instead of writing SQL statements to retrieve records from the database, we’d rather prefer to use objects.

The relational database information must be mapped to an object model. This can be done with an ORM tool and thankfully, Agile Toolkit comes bundled with its own ORM.

The ORM in Agile Toolkit is slightly different to other ORM systems out there. It's not only trying to provide object-oriented access to your database, but it also bundles all the business logic into those classes.

Agile Toolkit always gives preference to lightweight code instead of generating code. We are going to add a couple of files to destribe our models. Place this code into lib/Model/Job.php and lib/Model/Category.php. Do not forget to add <?php. It's common practice in Agile Toolkit not to close the php-tag (see PHP documentation, why)

class Model_Job extends Model_Table {
    public 
$entity_code 'job';
    function 
init(){
        
parent::init();

        
$this->addField('category_id')->refModel('Model_Category');
        
$this->addField('type');
        
$this->addField('company');
        
$this->addField('logo');
        
$this->addField('url');
        
$this->addField('position');
        
$this->addField('location');
        
$this->addField('description')->type('text');
        
$this->addField('how_to_apply')->type('text');
        
$this->addField('is_public')->type('boolean');
        
$this->addField('is_activated')->type('boolean');
        
$this->addField('email');
        
$this->addField('token');

        
$this->addField('created_dts')->type('timestamp')->system(true);
        
$this->addField('updated_dts')->type('timestamp')->system(true);
        
$this->addField('expires_at')->type('date')->system(true);
    }   
}
class Model_Category extends Model_Table {
    public 
$entity_code='category';
    function 
init(){
        
parent::init(); 

        
$this->addField('name');
    }
}       

If you are willing for Agile Toolkit to generate SQL CREATE code for you, you can set up a page for that. Code generator exists as a class, but you need to enable it in order to use it. Add file page/generate.php with the following content:

class page_generate extends Page_SchemaGenerator {}

Next navigate to http://localhost/jobeet/generate.html in your browser, pick Category model and click Generate button. The schema code will be shown on the right. You can also click "Generate" checkbox and "create" statement will be evaluated by sql automatically. Once you create 'Category' proceed to create 'Job'.

Your first CRUD

I am sure you are waiting until you can insert some data into your tables. That's what we will do now. Create file page/test.php and put the following code into it:

class page_test extends Page {
    function 
init(){
        
parent::init();
        
$this->add('CRUD')->setModel('Category');
        
$this->add('CRUD')->setModel('Job');
    }
}

Testing

Agile Toolkit takes a passive approach to testing. First of all - when developing with Agile Toolkit your code will break much less often. The UI code is quite well tested already, similarly with the ORM code. What you need to test is your own custom methods in your Models (with unit testing) and your custom UI code (with user testing). At this point we haven't done much, therefore we will add testing scripts some other day, when it will be necessary.

Database consistency

Agile Toolkit uses technique called "dbupdate". It stores in a special directory all "alter" statements to your database. Because Agile Toolkit only supports MySQL then maintaining upgrades is much simpler without abstraction. Using dbupgrade technique you can apply database updates consistently across all your software installations. Right now we are not going to create the scripts until our application gets a little bit more mature.

Final Thoughts

That’s all. By now you might start getting that thougth - "It's way too simple". Don't worry. You will get used to it.

Tomorrow we will talk about one of the most used paradigm in web frameworks, the MVC design pattern and how it is used in Agile Toolkit.