Path

ez components / documentation / api reference / 2006.2 / persistentobject


eZ Components 2006.2

PersistentObject: ezcPersistentSession

[ Tutorial ] [ Class tree ] [ Element index ] [ ChangeLog ] [ Credits ]

Class: ezcPersistentSession

ezcPersistentSession is the main runtime interface for manipulation of persistent objects. [source]
Persistent objects can be stored calling save() resulting in an INSERT query. If the object is already persistent you can store it using update() which results in an UPDATE query. If you want to query persistent objects you can use the find methods.

Properties

ezcDbHandler read  $database
The database handler set in the constructor.
ezcPersistentDefinitionManager read  $definitionManager
The persistent definition manager set in the constructor.

Method Summary

public ezcPersistentSession __construct( $db, $manager )
Constructs a new persistent session that works on the database $db.
public void addRelatedObject( $object, $relatedObject )
Create a relation between $object and $relatedObject.
public void createDeleteQuery( $class )
public ezcQuerySelect createFindQuery( $class )
Returns a select query for the given persistent object $class.
public void createUpdateQuery( $class )
public void delete( $pObject, $pObject )
Deletes the persistent object $pObject.
public void deleteFromQuery( $query )
public array(object) find( $query, $class )
Returns the result of the query $query as a list of objects.
public void findIterator( $query, $class )
public object getRelatedObject( $object, $relatedClass )
Returns the related object of a given class for an object.
public array(int=>object) getRelatedObjects( $object, $relatedClass )
Returns the related objects of a given class for an object.
public object load( $class, $id )
Returns the persistent object of class $class with id $id.
public object|null loadIfExists( $class, $id )
Returns the persistent object of class $class with id $id.
public void loadIntoObject( $pObject, $id )
Loads the persistent object with the id $id into the object $pObject.
public void refresh( $pObject )
Syncronizes the contents of $pObject with those in the database.
public void removeRelatedObject( $object, $relatedObject )
Removes the relation between $object and $relatedObject.
public void save( $pObject )
Saves the new persistent object $pObject to the database using an INSERT INTO query.
public void saveOrUpdate( $pObject )
Saves or update the persistent object $pObject to the database.
public void update( $pObject )
Saves the new persistent object $pObject to the database using an UPDATE query.
public void updateFromQuery( $query )

Methods

__construct

ezcPersistentSession __construct( $db, ezcPersistentDefinitionManager $manager )
Constructs a new persistent session that works on the database $db.
The $manager provides valid persistent object definitions to the session.

Parameters

Name Type Description
$db ezcDbHandler  
$manager ezcPersistentDefinitionManager  

addRelatedObject

void addRelatedObject( object $object, object $relatedObject )
Create a relation between $object and $relatedObject.
This method is used to create a relation between the given source $object and the desired $relatedObject. The related object is not stored in the database automatically, only the desired properties are set. An exception is {@ezcPersistentManyToManyRelation}s, where the relation record is stored automatically.

Parameters

Name Type Description
$object object The object to create a relation from.
$relatedObject object The object to create a relation to.

Throws

ClassDescription
ezcPersistentRelationOperationNotSupportedException if a relation to create is marked as "reverse".
ezcPersistentRelationNotFoundException if the deisred relation is not defined.

createDeleteQuery

void createDeleteQuery( $class )

Parameters

Name Type Description
$class  

createFindQuery

ezcQuerySelect createFindQuery( string $class )
Returns a select query for the given persistent object $class.
The query is initialized to fetch all columns from the correct table.
Example:
1.  $q $session->createFindQuery'Person' );
2.  $allPersons $session->find$q'Person' );

Parameters

Name Type Description
$class string  

Throws

ClassDescription
ezcPersistentObjectException if there is no such persistent class.

createUpdateQuery

void createUpdateQuery( $class )

Parameters

Name Type Description
$class  

delete

void delete( $pObject, object $pObject )
Deletes the persistent object $pObject.
This method will perform a DELETE query based on the identifier of the persistent object. After delete() the identifier in $pObject will be reset to null. It is possible to save() $pObject afterwords. The object will then be stored with a new id. If you defined relations for the given object, these will be checked to be defined as cascading. If cascading is configured, the related objects with this relation will be deleted, too.
Relations that support cascading are:
  • {@see ezcPersistenOneToManyRelation}
  • {@see ezcPersistenOneToOne}

Parameters

Name Type Description
$pObject object  
$pObject  

Throws

ClassDescription
ezcPersistentDefinitionNotFoundxception if $the object is not recognized as a persistent object.
ezcPersistentObjectNotPersistentException if the object is not persistent already.
ezcPersistentQueryException if the object could not be deleted.

deleteFromQuery

void deleteFromQuery( $query )

Parameters

Name Type Description
$query ezcQueryDelete  

find

array(object) find( $query, string $class )
Returns the result of the query $query as a list of objects.
Example:
1.  $q $session->createFindQuery'Person' );
2.  $allPersons $session->find$q'Person' );
If you are retrieving large result set, consider using findIterator() instead.
Example:

Parameters

Name Type Description
$query ezcQuerySelect  
$class string  

Throws

ClassDescription
ezcPersistentDefinitionNotFoundException if there is no such persistent class.
ezcPersistentQueryException if the find query failed

findIterator

void findIterator( $query, $class )

Parameters

Name Type Description
$query ezcQuerySelect  
$class  

getRelatedObject

object getRelatedObject( object $object, string $relatedClass )
Returns the related object of a given class for an object.
This method returns the related object of type $relatedClass for the object $object. This method (in contrast to {@see getRelatedObjects()}) always a single result object, no matter if more objects could be found (e.g. {@see ezcPersistentOneToManyRelation}).
Example:
1.  $person $session->load"Person");
2.  $relatedAddress $session->getRelatedObject$person"Address" );
3.  echo "Address of this person: " $relatedAddress->__toString();
Relations that should preferably be used with this method are:
  • {ezcPersistentManyToOneRelation}
  • {ezcPersistentOneToOneRelation}

Parameters

Name Type Description
$object object The object to fetch related objects for.
$relatedClass string The class of the related objects to fetch.

Throws

ClassDescription
ezcPersistentRelationNotFoundException if the given $object does not have a relation to $relatedClass.

getRelatedObjects

array(int=>object) getRelatedObjects( mixed $object, mixed $relatedClass )
Returns the related objects of a given class for an object.
This method returns the related objects of type $relatedClass for the object $object. This method (in contrast to {@see getRelatedObject()}) always returns an array of result objects, no matter if only 1 object was found (e.g. {@see ezcPersistentManyToOneRelation}).
Example:
1.  $person $session->load"Person");
2.  $relatedAddresses $session->getRelatedObjects$person"Address" );
3.  echo "Number of addresses found: " count$relatedAddresses );
Relations that should preferably be used with this method are:
  • {@see ezcPersistentOneToManyRelation}
  • {@see ezcPersistentManyToManyRelation}

Parameters

Name Type Description
$object mixed The object to fetch related objects for.
$relatedClass mixed The class of the related objects to fetch.

Throws

ClassDescription
ezcPersistentRelationNotFoundException if the given $object does not have a relation to $relatedClass.

load

object load( string $class, int $id )
Returns the persistent object of class $class with id $id.

Parameters

Name Type Description
$class string  
$id int  

Throws

ClassDescription
ezcPersistentException if the object is not available.
ezcPersistentException if there is no such persistent class.

loadIfExists

object|null loadIfExists( string $class, int $id )
Returns the persistent object of class $class with id $id.
This method is equivalent to load() except that it returns null instead of throwing an exception if the object does not exist.

Parameters

Name Type Description
$class string  
$id int  

loadIntoObject

void loadIntoObject( object $pObject, int $id )
Loads the persistent object with the id $id into the object $pObject.
The class of the persistent object to load is determined by the class of $pObject.

Parameters

Name Type Description
$pObject object  
$id int  

Throws

ClassDescription
ezcPersistentException if the object is not available.
ezcPersistentDefinitionNotFoundException if $pObject is not of a valid persistent object type.
ezcPersistentQueryException if the find query failed

refresh

void refresh( object $pObject )
Syncronizes the contents of $pObject with those in the database.
Note that calling this method is equavalent with calling loadIntoObject on $pObject with the id of $pObject. Any changes made to $pObject prior to calling refresh() will be discarded.

Parameters

Name Type Description
$pObject object  

Throws

ClassDescription
ezcPersistentException if $pObject is not of a valid persistent object type.
ezcPersistentException if $pObject is not persistent already
ezcPersistentException if the select query failed.

removeRelatedObject

void removeRelatedObject( object $object, object $relatedObject )
Removes the relation between $object and $relatedObject.
This method is used to delete an existing relation between 2 objects. Like {@see addRelatedObject} this method does not store the related object after removing its relation properties (unset), except for {@see ezcPersistentManyToManyRelation}s, for which the relation record is deleted from the database.

Parameters

Name Type Description
$object object Source object of the relation.
$relatedObject object Related object.

Throws

ClassDescription
ezcPersistentRelationOperationNotSupportedException if a relation to create is marked as "reverse".
ezcPersistentRelationNotFoundException if the deisred relation is not defined.

save

void save( object $pObject )
Saves the new persistent object $pObject to the database using an INSERT INTO query.
The correct ID is set to $pObject.

Parameters

Name Type Description
$pObject object  

Throws

ClassDescription
ezcPersistentException if $pObject is not of a valid persistent object type.
ezcPersistentException if $pObject is already stored to the database.
ezcPersistentException if it was not possible to generate a unique identifier for the new object
ezcPersistentException if the insert query failed.

saveOrUpdate

void saveOrUpdate( object $pObject )
Saves or update the persistent object $pObject to the database.
If the object is a new object an INSERT INTO query will be executed. If the object is persistent already it will be updated with an UPDATE query.

Parameters

Name Type Description
$pObject object  

Throws

ClassDescription
ezcPersistentException if $pObject is not of a valid persistent object type.
ezcPersistentException if any of the definition requirements are not met.
ezcPersistentException if the insert or update query failed.

update

void update( object $pObject )
Saves the new persistent object $pObject to the database using an UPDATE query.

Parameters

Name Type Description
$pObject object  

Throws

ClassDescription
ezcPersistentDefinitionNotFoundException if $pObject is not of a valid persistent object type.
ezcPersistentObjectNotPersistentException if $pObject is not stored in the database already.
ezcPersistentQueryException

updateFromQuery

void updateFromQuery( $query )

Parameters

Name Type Description
$query ezcQueryUpdate  

Last updated: Thu, 01 Nov 2007