Forums / Developer / relatedObjectsSlice

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