Author: Derick Rethans
Date: 2006-01-27 14:41:52 +0100 (Fri, 27 Jan 2006)
New Revision: 2059
Log:
- Fixed RST errors.
Modified:
packages/PersistentObject/trunk/docs/tutorial.txt
Modified: packages/PersistentObject/trunk/docs/tutorial.txt
===================================================================
--- packages/PersistentObject/trunk/docs/tutorial.txt 2006-01-27 13:40:22 UTC
(rev 2058)
+++ packages/PersistentObject/trunk/docs/tutorial.txt 2006-01-27 13:41:52 UTC
(rev 2059)
@@ -32,32 +32,33 @@
--------------------
We want to make a simple class, representing a person, persistent using
-persistent object. It is a simple class with only a few members:
+persistent object. It is a simple class with only a few members: ::
-::
- class Person
- {
- private id = null;
- public name = null;
- public age = null;
-
- public function getState()
+ <?php
+ class Person
{
- $result = array();
- $result['id'] = $this->id;
- $result['name'] = $this->name;
- $result['age'] = $this->age;
- return $result;
- }
+ private id = null;
+ public name = null;
+ public age = null;
- public function setState( array $properties )
- {
- foreach( $state as $key => $value )
+ public function getState()
{
- $this->$key = $value;
+ $result = array();
+ $result['id'] = $this->id;
+ $result['name'] = $this->name;
+ $result['age'] = $this->age;
+ return $result;
}
+
+ public function setState( array $properties )
+ {
+ foreach( $state as $key => $value )
+ {
+ $this->$key = $value;
+ }
+ }
}
- }
+ ?>
The id member will map to the required persistent identifier. It has to default
to null. This is not required for any of the other mapped members. The id field
@@ -103,29 +104,28 @@
ezcPersistentObjectDefinition, ezcPersistentObjectIdProperty and
ezcPersistentObjectProperty classes: ::
- <?php
- $def = new ezcPersistentObjectDefinition();
- $def->table = "persons";
- $def->class = "Person";
+ <?php
+ $def = new ezcPersistentObjectDefinition();
+ $def->table = "persons";
+ $def->class = "Person";
- $def->idProperty = new ezcPersistentObjectIdProperty;
- $def->idProperty->columnName = 'id';
- $def->idProperty->propertyName = 'id';
- $def->idProperty->generator = new ezcPersistentGeneratorDefinition(
- 'ezcPersistentSequenceGenerator' );
+ $def->idProperty = new ezcPersistentObjectIdProperty;
+ $def->idProperty->columnName = 'id';
+ $def->idProperty->propertyName = 'id';
+ $def->idProperty->generator = new ezcPersistentGeneratorDefinition(
'ezcPersistentSequenceGenerator' );
- $def->properties['name'] = new ezcPersistentObjectProperty;
- $def->properties['name']->columnName = 'full_name';
- $def->properties['name']->propertyName = 'name';
- $def->properties['name']->propertyType =
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+ $def->properties['name'] = new ezcPersistentObjectProperty;
+ $def->properties['name']->columnName = 'full_name';
+ $def->properties['name']->propertyName = 'name';
+ $def->properties['name']->propertyType =
ezcPersistentObjectProperty::PHP_TYPE_STRING;
- $def->properties['age'] = new ezcPersistentObjectProperty;
- $def->properties['age']->columnName = 'age';
- $def->properties['age']->propertyName = 'age';
- $def->properties['age']->propertyType =
ezcPersistentObjectProperty::PHP_TYPE_INT;
+ $def->properties['age'] = new ezcPersistentObjectProperty;
+ $def->properties['age']->columnName = 'age';
+ $def->properties['age']->propertyName = 'age';
+ $def->properties['age']->propertyType =
ezcPersistentObjectProperty::PHP_TYPE_INT;
- return $def;
- ?>
+ return $def;
+ ?>
The first block of code creates the definition object and sets the database
table and the name of the class to map. The second block defines the mapping of
@@ -157,8 +157,12 @@
The session object is in charge of the actual loading and saving of persistent
objects. A session can be created simply by instantiating it: ::
- $session = new ezcPersistentSession( ezcDbInstance::get(),
- new ezcPersistentCodeManager(
"path/to/definitions" ) );
+ <?php
+ $session = new ezcPersistentSession(
+ ezcDbInstance::get(),
+ new ezcPersistentCodeManager( "path/to/definitions" )
+ );
+ ?>
The session takes two arguments: a pointer to the database instance to use and
the manager it should use to retrieve persistent object definitions. We are
@@ -174,11 +178,13 @@
-------------------------------
Creating a new Person object and making it persistent is straight forward: ::
- $object = new Person();
- $object->name = "Guybrush Threepwood";
- $object->age = 31;
+ <?php
+ $object = new Person();
+ $object->name = "Guybrush Threepwood";
+ $object->age = 31;
- $session->save( $object );
+ $session->save( $object );
+ ?>
This code saves our newly created object to the database and generates an id
for it. The id is set to the id property of the object. Since Guybrush is our
@@ -188,8 +194,10 @@
probably he is younger than 31. To make the change simply edit the object and
tell the session to update it. ::
- $object->age = 25;
- $session->update( $object );
+ <?php
+ $object->age = 25;
+ $session->update( $object );
+ ?>
Note that we used update() to store the object this time. This is because we
want to trigger an UPDATE query instead of an INSERT query.
@@ -200,7 +208,9 @@
There are several ways to retrieve persistent objects from the database. The
simplest is to fetch one object by its identifier. ::
- $object = $session->load( 'Person', 1 );
+ <?php
+ $object = $session->load( 'Person', 1 );
+ ?>
This code retrieves the Guybrush object created above.
@@ -208,11 +218,13 @@
list you can use the find method. The find method requires a query parameter
which you
can retrieve from the session first. ::
- $q = $session->createFindQuery( 'Person' );
- $q->where( $q->expr->gt( 'age', 15 ) )
- ->orderBy( 'full_name' )
- ->limit( 10 );
- $objects = $session->find( $q, 'Person' );
+ <?php
+ $q = $session->createFindQuery( 'Person' );
+ $q->where( $q->expr->gt( 'age', 15 ) )
+ ->orderBy( 'full_name' )
+ ->limit( 10 );
+ $objects = $session->find( $q, 'Person' );
+ ?>
This code will fill fetch a maximum of 10 Person objects older than 15 years
sorted by their names.
@@ -222,17 +234,19 @@
want it to be fast and efficient. For this you can use the fetchIterator()
method: ::
- $q = $session->createFindQuery( 'Person' );
- $q->where( $q->expr->gt( 'age', 15 ) )
- ->orderBy( 'name' )
- ->limit( 10 );
- $objects = $session->findIterator( $q, 'Person' );
-
- foreach( $objects as $object )
- {
- ...
- }
+ <?php
+ $q = $session->createFindQuery( 'Person' );
+ $q->where( $q->expr->gt( 'age', 15 ) )
+ ->orderBy( 'name' )
+ ->limit( 10 );
+ $objects = $session->findIterator( $q, 'Person' );
+ foreach( $objects as $object )
+ {
+ // ...
+ }
+ ?>
+
This code will produce the same result as the first find() example. However,
only one object will be instantiated and the data will be transfered from the
database only when it is needed.
@@ -243,16 +257,20 @@
The easiest way to delete persistent objects is to use the delete method() on
the session: ::
- $object = $session->load( 'Person', 1 );
- $session->delete( $object );
+ <?php
+ $object = $session->load( 'Person', 1 );
+ $session->delete( $object );
+ ?>
Of course, you can only delete instantiated objects this way. If you want to
delete an object or a whole series of objects that are not instantiated you can
use the deleteFromQuery() method: ::
- $q = $session->createDeleteQuery( 'Person' );
- $q->where( $q->expr->gt( 'age', 15 ) );
- $session->deleteFromQuery( $q );
+ <?php
+ $q = $session->createDeleteQuery( 'Person' );
+ $q->where( $q->expr->gt( 'age', 15 ) );
+ $session->deleteFromQuery( $q );
+ ?>
When executed the above code will remove all persons older than 15 years old
from the database.
@@ -287,8 +305,12 @@
If your database requires you to use a sequence this parameter should be
provided to the ezcPersistentSequenceGenerator in the mapping definition. ::
- $def->idProperty->generator = new ezcPersistentGeneratorDefinition(
'ezcPersistentSequenceGenerator',
- array(
'sequence' => 'person_sequence' ) );
+ <?php
+ $def->idProperty->generator = new ezcPersistentGeneratorDefinition(
+ 'ezcPersistentSequenceGenerator',
+ array( 'sequence' => 'person_sequence' )
+ );
+ ?>
Definition loaders
@@ -313,13 +335,15 @@
It is very easy to create your own definition loader. Simply extend the
ezcPersistentDefinitionManager abstract class and implement the fetchDefinition
method: ::
-
- class ezcPersistentCodeManager extends ezcPersistentDefinitionManager
- {
- public function fetchDefinition( $class )
+
+ <?php
+ class ezcPersistentCodeManager extends ezcPersistentDefinitionManager
{
+ public function fetchDefinition( $class )
+ {
+ }
}
- }
+ ?>
The fetchDefinition() method should create the definition structure for the
requested class or throw an exception
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components