Object persistence
Getting objects to persist between page views is cumbersome since you have to input all
elements as HTTP form types and read them on the next view. By using eZHTTPPersistence
it's possible to fetch these data automatically. The class can also be used to split
selected items from unselected ones.
Filling in data from forms
The HTML form
The HTML form is created by inputting the data elements as hidden inputs.
<form type="post">
<input type="hidden" name="MyData_title[]" value="title1" />
<input type="hidden" name="MyData_title[]" value="title2" />
<input type="hidden" name="MyData_title[]" value="title3" />
</form>
The definition
We then create a definition so that data is properly input.
Fields define the mapping from attributes to member variables and keys
contain an array of attributes which are defined as keys (keys are not overridden).
$my_def = array( "fields" => array( "id" => "ID",
"title" => "Title" ),
"keys" => array( "id" ) );
The code
Finally we fetch the original objects from the DB and override the data with the form data
// Fetch the objects from the DB
$objects =&t; fetch_objects();
// Fetch the HTTP data with "MyData" as the base
eZHTTPPersitence::fetch( "MyData", $my_def, $objects, eZHTTPTool::instance(), true );
Splitting data
The HTML form
The HTML form consists of a number of checkboxes with a specific name.
<form type="post">
<input type="checkbox" name="MyData_id_checked[]" value="1" />
<input type="checkbox" name="MyData_id_checked[]" value="5" />
<input type="checkbox" name="MyData_id_checked[]" value="42" />
</form>
The code
In the code we fetch all the existing objects and split them into two arrays,
the objects in the last array are then removed.
// Fetch the objects from the DB
$objects =&t; fetch_objects();
// Fetch the HTTP data with "MyData" as the base
eZHTTPPersitence::splitSelected( "MyData", $objects, eZHTTPTool::instance(), "id",
$keep_objs, $remove_objs );
foreach( $remove_objs as $obj )
{
$obj->remove();
}
|