Control output of object relations

Author Message

Stephen Martin

Monday 03 March 2008 3:35:53 pm

New user question. I've created a class called "animal" and am using a object relations to store images associated with the animal.

Using the code below I can output the photos, however, I'd like to output just the first photo instead of all of them. I've searched the forum carefully but cannot find a proper solution. Please advise. Thanks!

		{def $nodes=fetch( 'content', 'list',
        hash( 'parent_node_id', 59,
              'depth', 1 ) )}
 		{foreach $nodes as $node}
			<div id="animal-listing">
				<div id="animal-head">
					<h1>{$node.name|wash} {attribute_view_gui attribute=$node.object.data_map.genestar_rating}/{attribute_view_gui attribute=$node.object.data_map.genestar_rating_max} <img style="vertical-align:middle" src={'genestar-logo.gif'|ezimage()} alt="GeneStar Logo" /></h1>
					<div id="price">{attribute_view_gui attribute=$node.object.data_map.price}</div> {* price *}
				</div> {* animal-head *}
				<div id="animal-photo">
					{attribute_view_gui attribute=$node.object.data_map.photography}
				</div> {* animal-photo *}
			</div> {* animal-listing *}
		{/foreach}

Felix Laate

Tuesday 04 March 2008 12:37:50 am

Hi Stephen,

if you want he first image (dependning on the sorting):

{def $nodes=fetch( 'content', 'list',
                              hash( 'parent_node_id', 59,
                             'depth', 1,
                              'limit',1 ) )}

Or do you have other criteria?

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Stephen Martin

Tuesday 04 March 2008 8:39:38 am

Felix,

Thanks for your answer. It wasn't quite was I was looking for. Adding the limit does return one record, however, I'm looking to return the first image of an object relation. So, if I put the limit of 1 in there, I get just one record returned whereas I'd like all records (animals) displayed with the first related photo.

$node.object.data_map.photography is an object relations that contains multiple photos. I need a way to get just the first image out of it.

Hopefully that is clearer.

Thanks!

Xavier Dutoit

Wednesday 05 March 2008 4:41:19 am

{$node.object.data_map.photography|attribute(show,2)}

Will help you finding what you want

http://www.sydesy.com

Stephen Martin

Thursday 06 March 2008 9:11:47 am

Xavier,

Thanks for the reply. That tip helped me figure it out. Don't know if this is the most efficient solution, but this is the code:

{foreach $node.data_map.photography.content.relation_list as $relation}
     {def $obj=fetch('content', 'object',
			hash('object_id', $relation.contentobject_id))}
			{attribute_view_gui attribute=$obj.data_map.image image_class='medium'}		
	{undef $obj}
	{break}
{/foreach}

André R.

Thursday 06 March 2008 9:56:04 am

That will do fine, but just so you know it this will contain objects in the trash as well.
In newer eZ Publish versions there is attribute on the relation called ''in_trash" or something.
On older versions (and new ones) it is visible on the main_node_id of the object you fetch (it will be false / null / empty).

One alternative to looping over the relations list is to use the fetch('content', 'relation') (see doc).

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 30 2025 21:18:12
Script start
Timing: Jan 30 2025 21:18:12
Module start 'layout'
Timing: Jan 30 2025 21:18:12
Module start 'content'
Timing: Jan 30 2025 21:18:12
Module end 'content'
Timing: Jan 30 2025 21:18:12
Script end

Main resources:

Total runtime0.0345 sec
Peak memory usage8,192.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0053 588.2344151.2109
Module start 'layout' 0.00530.0036 739.4453220.6875
Module start 'content' 0.00890.0239 960.13281,005.9297
Module end 'content' 0.03280.0016 1,966.062537.9922
Script end 0.0344  2,004.0547 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00277.6931140.0002
Check MTime0.00102.8121140.0001
Mysql Total
Database connection0.00071.916010.0007
Mysqli_queries0.00205.943930.0007
Looping result0.00000.026310.0000
Template Total0.00102.910.0010
Template load0.00072.022610.0007
Template processing0.00030.841410.0003
Override
Cache load0.00051.395710.0005
General
dbfile0.004914.324880.0006
String conversion0.00000.014540.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