Share » Learn » eZ Publish » The PersistentObject eZ Component:...

The PersistentObject eZ Component: Putting Relations Where Relations Belong

Tuesday 27 February 2007 12:12:00 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

We will create a simple address book that allows you to manage information about your friends. The application will have a web interface that facilitates the creation and deletion of objects. For simplicity, the manipulation of existing objects will be left out.

The database layout

The application relies on five database tables: The person table stores the first and last name of each person. A second table, called detail, contains additional details about people. The table email is used to store email addresses, and the table address stores physical addresses. Because a person can be assigned to several addresses and several people can live at one address, we have a relation table: person_address.

-- phpMyAdmin SQL Dump
-- version 2.6.3-rc1
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generation Time: Jan 08, 2007 at 11:26 AM
-- Server version: 5.0.30
-- PHP Version: 5.1.6-pl10-gentoo
-- 
-- Database: `ezccontact`
-- 
 
-- --------------------------------------------------------
 
-- 
-- Table structure for table `address`
-- 
 
CREATE TABLE `address` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `street` varchar(255) NOT NULL,
  `zip` varchar(5) NOT NULL,
  `city` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
 
-- --------------------------------------------------------
 
-- 
-- Table structure for table `detail`
-- 
 
CREATE TABLE `detail` (
  `person` int(11) NOT NULL,
  `birthday` int(11) DEFAULT NULL,
  `comment` varchar(255) DEFAULT NULL,
  PRIMARY KEY  (`person`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 
-- --------------------------------------------------------
 
-- 
-- Table structure for table `email`
-- 
 
CREATE TABLE `email` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `person` int(11) NOT NULL,
  `email` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
 
-- --------------------------------------------------------
 
-- 
-- Table structure for table `person`
-- 
 
CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
 
-- --------------------------------------------------------
 
-- 
-- Table structure for table `person_address`
-- 
 
CREATE TABLE `person_address` (
  `person` int(11) NOT NULL,
  `address` int(11) NOT NULL,
  PRIMARY KEY  (`person`,`address`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

This database design contains several relations between objects. The person table (represented by a person object) is the central instance in the application and represents the starting point for all of our relations.

  • The detail table contains information describing exactly one person. Every person can only have one detail record. Here, we have the simplest kind of relation: a 1:1 (one-to-one) relation.
  • A person can have multiple email addresses (how many do you have?), but one email address can only belong to one person (at least in our example). Here, we have the most common relation type: a 1:n (one-to-many) relation.
  • A person can have multiple physical addresses (for example, a home address and an office address) and multiple persons can be assigned to the same address. This reflects the most complex relation type: a n:m (many-to-many) relation.

The application structure

The example application follows a very simple MVC (Model-View-Controller) pattern: The main file is index.php, which defines and runs the main controller. This controller responds to several "actions", and dispatches them to an action class, which will then handle the request. In addition, the controller handles the initialization and processing of a template (which is defined by the action object).

We will not discuss the controller and template mechanism in this article, but will concentrate on the configuration and usage of the PersistentObject component. If you download the application source, you can find the action classes in the directory actions/. We will also deal with the model classes used in the application (in the directory models/) and the definition files used by the PersistentObject component (in the directory persistent/).

The other directories are not discussed in this tutorial. Here is a brief summary of what they contain:

autoload/
This directory contains the autoload files for our application, which are used in combination with the eZ Components autoload mechanism.

docs/
You can find setup information for the application here. For example, there is the database structure and some example data as MySQL dumps.

templates/
The example application relies on the Template component, for which the HTML template files can be found here.

templatesc/
Because the Template component compiles template code into PHP source code, this directory is needed to cache the generated code.

36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)