dbForm PHP Project
Serializing Demonstration

Serializing allows an object to be converted to a format that is considered portable. This means that once serialized the object can be stored and then recreated later.
The most useful use for this would be to store the dbForm instance in a file or in a session and then recreated later.

This is what we are doing in this example.


Person
Search:
 
Source Code

We are using the serialize and unserialize function to store an instance of the dbForm object in the session. This means that dbForm only have to look at the table definition the first time it is run for a given session and from then on it can be recreated from teh serialized data.

When serialized dbForm is able to store almost all of the information about the form so that it will do the minimal processing each time.

Lets look at this example to see what it does.

Firstly we start the session and check to see if the dbForm object exists in the session.
If not then we create the dbForm object. We create this as normal with one exception, most if not all of the commands that we use to set the form up can now be called BEFORE we call processForm. This includes the template.
Then once the object is created and before we process the form we store it in the session.

If the dbForm object does exist in the session then we simply recreated it by unserializing it.

Now once we have the dbForm object created, either initially or from the session, we call processForm and dispayForm as per normal.

Serializing does not work well if you need to have parts of the form modified at run time based on, for example, the form action.

View source code for demo7.php

Template File

The template for this demonstration is fairly straight forward however it does use a new feature introduced in dbForm 0.8.2. It has the ability to incorporate attributes in the dbForm tags.

For example if we want a field to be displayed with a class and a specific style we can now incorporate that into the template rather than having to specify it in code. This makes the templates as flexible as possible.

In this example we are adding a class and style to a named field tag
{:field="firstname" class="content2" style="width:140px;"}

This will be replaced in the output html as something like:
<input type="text" name="firstname" value="Tony" class="content2" style="width:140px;" />

This allows almost all formatting to be controlled via the template if required.

View source code for demo7.tpl

Download Demo files

The files that have been used in this demonstration can be downloaded so you can try them out for yourself.

demo7.zip