PX:Core Features Tutorial

From Peroxide

Jump to: navigation, search

Contents

Introduction

This tutorial is the second in our series of tutorials and it is designed to show you the core features of perOXide. If you haven't read it so far, it is advisable that you have a look at the Basic Tutorial as the stuff in here is based on the examples given there.

What we have so far

In the Basic Tutorial we have developed a very simple guest book which allowed our users to post comments. The implementation was very simple, however it had a lot of drawbacks which we are going to tackle in this tutorial:

  • There should be more attributes available for a posting such like:
    • Submit date
    • Name of the poster
  • There is no paging so all the comments pile up.
  • All database entries are constantly fetched from the database though they rarely change.
  • You cannot modify or delete entries.
  • The HTML is hard coded into the pages making it hard to adapt the guest book for different sites.


A better implementation

As you can see, our guest book so far is a quite poor implementation. This tutorial is going to show you how you can create a much better implementation with little work using perOXide.

More attributes

Firstly, let's add the submit date and the name of poster to the attributes of the posting. Open up the GuestbookEntry.php snippet we have created in the previous tutorial. Adding more attributes is quite simple, we add two more member variables, set up the type of these variables in the tableDefinition function and add getter and setter functions for these two:

class GuestbookEntry extends DBObject {

    ...

    private $postedat;
    private $postername;

    ...

    // this tells perOXide, how your table is built up
    public function tableDefinition() {
        return
        "postedat I8,". // posted at  - this is a timestamp (integer 8)
        "postername C(128),".  // name of the poster this is a (var)char of length 128
        "content XL"; // we have one field named "content" of type XL (which is Text or Clob)
    }

    // getters and setters omitted for brevity.
    ...
}

That's about it. Now you might wonder, who is going to update your table in the database. Remember the setup page that we created in the previous tutorial? Just run it again and see - your table is updated automatically! When you call createTable on any DBObject instance, perOXide will check the table in the database and will add columns when they are missing in the table. It will however not delete any existing columns that are not defined in the DBObject for obvious reasons. In case you want to remove columns, you have to delete them manually.

Personal tools