relatedObjectsSlice

Author Message

*- pike

Wednesday 09 September 2009 7:44:20 am

I noticed relatedobject and reverserelatedobjects fetches dont implement offset and limit:
http://ez.no/doc/ez_publish/technical_manual/4_0/reference/modules/content/fetch_functions/related_objects
http://ez.no/doc/ez_publish/technical_manual/4_0/reference/modules/content/fetch_functions/reverse_related_objects

Since fetching and slicing them in tpl seemed to be heavy, i moved that functionality to php. For anyone who's interested, here's the code. Note you need to have an extension available where you can tuck this in. In a class definition that holds some static functions, you'll add the php fetch. In your extensions function definition, you reference that. In your template, you call a fetch. In detail:

In your libraries class somewhere, add

	static function fetchRelatedObjectsSlice(
		$object_id,
		$attribute_identifier=0,
		$all_relations=false,
		$offset=0, $limit=0, $reverse=false,
		$group_by_attribute=true,    
		$sort_by=null,
		$ignore_visibility=true,
		$istplfetch=false
	) {

		eZDebug::writeDebug("fetchRelatedObjectsSlice","MyLibraryLib");

		/*
			should do the same as related_objects
			http://ez.no/doc/ez_publish/technical_manual/4_0/reference/modules/content/fetch_functions/related_objects
			http://ez.no/doc/ez_publish/technical_manual/4_0/reference/modules/content/fetch_functions/reverse_related_objects
			but implements a limit, offset, reverse. 
			its a lot quicker to slice an array in php than in tpl.
			
			the strange defaults mimic the ezp fetches defaults, too
			
		*/
		
		$params = array();
		$params['AllRelations']		= $all_relations;
		$params['SortBy'] 			= $sort_by;
		$params['IgnoreVisibility'] = $ignore_visibility;
				
		$object = eZContentObject::fetch($object_id);
		
		$allrelated = $object->relatedObjects( 
			false, 
			false, 
			$attribute_identifier, 
			$group_by_attribute, 
			$params, 
			$reverse
		);
		$related = ($limit)?array_slice($allrelated,$offset,$limit):array_slice($allrelated,$offset);
		if ($istplfetch) return array( 'result' => $related );
		else return $related;
		
	}
	

In the function_definition of your extension, add

	$FunctionList['related_objects_slice'] = array(
			'name' => 'related_objects',
			'operation_types' => array( 'read' ),
			'call_method' => array(
				'include_file' => 'path_to_your_class_lib.php',
				'class' => 'your_class_name',
				'method' => 'fetchRelatedObjectsSlice'
			),
			'parameter_type' => 'standard',
			'parameters' => array(
				array(
					'name' => 'object_id',
					'type' => 'integer',
					'required' => true
				),
				array(
					'name' => 'attribute_identifier',
					'type' => 'integer',
					'required' => false,
					'default' => 0
				),
				array(
					'name' => 'all_relations',
					'type' => 'boolean',
					'required' => false,
					'default' => false
				),
				array(
					'name' => 'offset',
					'type' => 'integer',
					'required' => false,
					'default' => 0
				),
				array(
					'name' => 'limit',
					'type' => 'integer',
					'required' => false,
					'default' => 0
				),
				array(
					'name' => 'reverse',
					'type' => 'boolean',
					'required' => false,
					'default' => false
				),
				array(
					'name' => 'group_by_attribute',
					'type' => 'boolean',
					'required' => false,
					'default' => false
				),
				array(
					'name' => 'sort_by',
					'type' => 'array',
					'required' => false,
					'default' => null
				),
				array(
					'name' => 'ignore_visibility',
					'type' => 'boolean',
					'required' => false,
					'default' => true
				),
				array(
					'name' => 'istplfetch',
					'type' => 'boolean',
					'required' => false,
					'default' => true
				)
			)
	);

And in your template, call

{set relnodes = fetch('myextension','related_objects_slice',hash(
	'object_id',$node.object.id,'all_relations',true(),
	'offset',$offset,'limit',$limit,'reverse',true(),
	'ignore_visibility',false()
))}

Note it is only slightly quicker than the original calls; it still fetches all objects, but it slices the array in php, thats all.

$2c,
*-pike

---------------
The class eZContentObjectTreeNode does.

André R.

Wednesday 09 September 2009 11:21:10 am

Note that limit and offset is implemented in 4.1, its just waiting for documentation (should be soon as we have a new doc writer).
ref: http://issues.ez.no/IssueView.php?Id=12651&activeItem=3

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 19:31:33
Script start
Timing: Jan 18 2025 19:31:33
Module start 'layout'
Timing: Jan 18 2025 19:31:33
Module start 'content'
Timing: Jan 18 2025 19:31:33
Module end 'content'
Timing: Jan 18 2025 19:31:33
Script end

Main resources:

Total runtime0.0150 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0059 587.8125152.6094
Module start 'layout' 0.00590.0026 740.421939.4141
Module start 'content' 0.00850.0047 779.835993.4297
Module end 'content' 0.01320.0017 873.265634.3047
Script end 0.0148  907.5703 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002315.5781140.0002
Check MTime0.00106.7397140.0001
Mysql Total
Database connection0.00095.966510.0009
Mysqli_queries0.002818.739630.0009
Looping result0.00000.066810.0000
Template Total0.00149.010.0014
Template load0.00074.811410.0007
Template processing0.00064.197210.0006
Override
Cache load0.00053.237810.0005
General
dbfile0.00021.616580.0000
String conversion0.00000.058940.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs