Online Shop

phpersistence tutorial and demo application

This is a simple application with source codes to demonstrate usage of phpersistence.

The following scripts demonstrate business processes that make use phpersistence.

Important : it is important to use the scripts in the right order to keep integrity of data (ids have to be created in the correct order because they are hard coded in this example).
If you try this online (on www.phpersistence.org, not on your own server), different other users may use this at the same time, preventing it from working as expected.

To see more detailed explanations and help, together with the source code of the scripts, clic on "source".
Hit the back button to come back to this index.

0. The persistent classes

Persistable classes do not need to inherit a special base class or do not need extra tags.
The only requirement is that they have a member that represents a unique identifier, for example a field "id" or "name".
In fact, most applications (esp. web applications) need to distinguish objects by identifier, so you could consider this a feature rather than a limitation.

There are 5 persistent classes in this application :
- User : a user (source)
- Cart : a shopping cart (source)
- Product (source) : a product with 2 subclasses : Wine (source) and Shirt (source)

1.  Initialisation

First of all we empty the tables to start a clean demo.
Script : init
Source : source

2. Creation of a user

We create a User object and save it.
We imagine the data coming from a form where a user can create a user account.
Script : create-user.php
Source : source

3. Find the user

We use the query by example (QBE) technique to retrieve the user by its email.

A QBE-style query is guided by an example, or template, object. More specifically, you perform a query by creating a template object, populating its fields with the values you want matched, showing the template object to the query system and saying, “See this? Go get all the objects that look like this one.”

In this first script we find a single user by his email :
Script : get-user-by-mail.php
Source : source

In our application a user has an array of phone numbers. In this script we find a user by one of his phone numbers.
We retrieve the result as an array of User objects (even if it is unlikely that more than one user has the same phone number).
Script : get-user-by-phone.php
Source : source

4. Create products

We create product objects that use inheritence.
Let's imagine the data comes from a back-office.
Creation of 2 wines : create-wines.php (source)
Creation of 2 shirts : create-shirts.php (source)

5. Listing products

Now we have some products in our database. There are multiple simple ways to get product lists.
In the first example, we get an array of all products, sorted by price.
To speed up the process, the objects are not fully instanciated, we get only base class objects.
Script : get-products.php
Source : source

In this example, we an array of Wines. The objects are complete, including inherited and linked objects.
Script : get-wines.php
Source : source

6. Display a product

Just a simple script to show how to get a object by its unique id.
Script : get-one-wine.php
Source : source

It is also possible to get an array of objects using an array of identifiers.
Script : get-two-shirts.php
Source : source

7. Let's go shopping

First we create a cart for a specific user.
Script : create-cart.php
Source : source

Then, we look up the cart in the database. We use a query by example to get the cart by carts owner's email.
Of course in the real world the user would stay in the session, without the need to retrieve it again. Its just to demonstrate the cascading query by example.
Script : get-user-cart.php
Source : source

Now we can add products to the cart.
fill-cart.php (source)

8. cascading updates

It is simple to update objects whenever needed.
Lets replace the first product in this cart (without affecting the second one).
By the way, we add a phone number to the user, to demonstrate the "cascading" of saving / updating objects.
When we save the cart, the user is also updated, because the objects are linked, and this link is configured as "cascade all" (see link to phpersistence configuration file at the bottom).
Learn more in the source comment of this script.
 : change-cart.php (source)

Here is how the cart is filled now :
show-cart.php (source)

9. Deleting objects

We empty the cart : empty-cart.php (source)

deletion of the cart : delete-cart.php (source)

10. Appendix

Database dump for this application (MySQL example): database/shop_demo.sql

Annotated configuration file : conf/persistence.conf.php