PersistenceObjects for php

Manual

CRUD (create, read, update, delete) operations:

Using PersistenceObjects is a matter of understanding class extensions. All CRUD operations are performed using PersistenceObjects and the rest of the required database methods propably end up using the same methods in the end (unless optimization is required).

PersistenceObject-class is initialised with a basic constructor that can take three different types of parameters:

After initialisation, basic CRUD-operations are performed as follows.

Create

//create an object-instance.
$user = new User(); 
//set desired values. ID does not need to be set. PersistenceObjects will do that for you.
$user->setUserName("luuseri"); 
$user->setUserRole(3); 
// initialise the PersistenceObject with the newly created object.
$up = new UserPersistence($user); 
//call persist for newly created objects.
$up->persist(); 

Read one object

When creating a PersistenceObject-instance, a cache is searched for the given id. Cache is used if the given id is found. The cache is updated whenever an update occurs and the id is removed from the cache whenever the object is deleted.

// initialise the PersistenceObject with the desired id.
$up = new UserPersistence($userId);
// Since initalisation already fetched the object, we can call getValueObject() to get the object-instance.
$user = $up->getValueObject();

Alternatively we can fetch a fresh object from database.

$up = new UserPersistence($userId);
//the second parameter instructs the PersistenceObject to skip cache.
$user = $up->get($userId, true);

Read all objects

// initialise the PersistenceObject with no parameters.
$up = new UserPersistence();
// call getAll() to get all instances of the object class.
$userList = $up->getAll();

Update

// initialise the PersistenceObject with the desired id.
$up = new UserPersistence($userId);
// Since initalisation already fetched the object, we can call getValueObject() to get the object-instance.
$user = $up->getValueObject();
// set the updated values.
$user->setUserName("edited_luuseri");
$user->setUserRole(0);
// Since the PersistentObject references the edited user we can reuse the same instance.
// Call merge to perform updates on an edited object.
$up->merge();

Delete

// initialise the PersistenceObject with the desired id.
$up = new UserPersistence($userId);
// Call delete to remove the object permanently from the database and memory.
$up->delete();
back to main page