excel export: how to spare memory in tpl ?

Author Message

Seb Gaillard

Wednesday 22 July 2009 12:10:58 pm

Hi,

I want to make an excel export of all content of a class in the same page.
I have some memory limit problem :

<b>Fatal error</b>:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 120061917 bytes) in <b>/var/www/psycho/var/mySite/cache/template/compiled/partners-68dce9a3d2b5c693cca5f961b1369a56.php</b> on line <b>688</b><br />

my excel file is generated by clicking on a link like this

<a href={concat('layout/set/excel/content/view/partners/(class)/mySite_interview')|ezurl()}>Interview</a>

As there is no possibility to navigate between differents pages (with offset parameter), I try to "simulate" the pagination mechanism like that

{def   $depth= 7
        $parentNodeId = 2
        $counter=0
	$offset=0
	$limit=25
	$filter_hash=true()
     $count = fetch('content','list_count',hash('parent_node_id', $parentNodeId,
								'main_node_only', true(),
								'depth', $depth,
								'class_filter_type', include,
								'class_filter_array', array($view_parameters.class )
								)
					)
     $ForLimit = ceil($count|div($limit))
     }
<html>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<table style="border: 1px solid black">
<tr>
<td colspan="3" style="font-weight: bold; background-color: silver;">
Article [{$count}]
</td>
</tr>
<tr>
    <td></td>
    <td style="font-weight: bold;">Type of content</td>
    <td style="font-weight: bold;">Title</td>
</tr>
{for 0 to $ForLimit}
	{set $offset=$offset|sum($limit)
	$filter_hash=hash('parent_node_id', $parentNodeId,
						'main_node_only', true(),
						'limit', $limit,
						'offset', $offset,
				        'class_filter_type', 'include',
				        'class_filter_array', array( $view_parameters.class ),
				        'depth', $depth,
						)

	}
	{* by creating a new set of $elements, I hope to spare memory*}
        {def $elements=fetch('content','list',$filter_hash)}
	{foreach $elements as $element}
		<tr>
		    {set $counter = inc( $counter )}
		    <td>{$counter}</td>
		    <td>{$element.class_identifier|wash()}</td>
		    <td>{$element.name|wash()}</td>
		</tr>
	{/foreach}
	{* free memory ???*}
	{undef $elements}
{for}
</table>
</html>

But it doesn't work. Someone has a solution?

Max Keil

Wednesday 22 July 2009 4:41:49 pm

Hi Seb,

your last 3 line looks ike this! I see, that you forgot to close the "for" method?

{undef $elements}
{for}
</table>
</html>

For exporting huge information containers, I would prefer to use native PHP functions instead of Template code!

It's also not clean to def the same variable within the {for}{/for}. Use "set" for this and def the variable before the loop.

Mit freundlichen Grüßen
Best regards

Max Keil

____________________________________________________________
eZ Publish Gold Partner - http://www.all2e.com
http://ez.no/partners/worldwide_partners/all2e_gmbh

Seb Gaillard

Thursday 23 July 2009 12:35:39 am

Hi Max,

Effectively I forgot to close the for method... Now, it work's fine! I think that after a lot of hours with the nose through the code, I didn't see anything...

Otherwise, I create (def) the $elements varliable in each loop because I hope that "{undef}" free memory. Do you know:
1/ If it's true ?
2/ if I use {set ...}, the memory is spared too?

Do you know where can I find an exemple who "use native PHP functions instead of Template code" for doing this ?

Thank's,

Seb

Max Keil

Thursday 23 July 2009 5:43:53 pm

You're right,

undef should be more memory efficient.

native PHP: you can create a simple template fetch function or module which would do the same work for you and generate the html, but much more efficient - because of the loops and fetches.

Mit freundlichen Grüßen
Best regards

Max Keil

____________________________________________________________
eZ Publish Gold Partner - http://www.all2e.com
http://ez.no/partners/worldwide_partners/all2e_gmbh

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