Associative arrays not working?

Author Message

Oliver Frommel

Thursday 07 August 2008 6:49:11 am

Hello,

yesterday I tried to extend the Collected information Export extension (http://projects.ez.no/cie) to export the ezoption datatype. In that course I discovered some strange behaviour where I could not reference an associative array as I thought I could.

class eZOptionHandler extends BaseHandler{
       function exportAttribute(&$attribute, $seperationChar) {
             echo $array['DataInt'];
             ...

that last statement never produced any output, whereas var_dump($attribute) shows:

object(ezinformationcollectionattribute)(10) {
  ["PersistentDataDirty"]=>
  bool(false)
  ["Content"]=>
  NULL
  ["ID"]=>
  string(5) "15178"
  ["InformationCollectionID"]=>
  string(5) "11191"
  ["ContentClassAttributeID"]=>
  string(4) "1005"
  ["ContentObjectAttributeID"]=>
  &string(6) "172424"
  ["ContentObjectID"]=>
  &string(5) "20112"
  ["DataText"]=>
  string(0) ""
  ["DataInt"]=>
  string(1) "3"
  ["DataFloat"]=>
  string(1) "0"
}

I have worked around that problem by iterating over the keys of the structure:

foreach ($attribute as $key => $value) {
      if ($key == 'DataInt') {
           $option_value = $value;
      }
}

I remember somewhere in the back of my mind to have read something about variables/datatypes getting "loaded" from the db only "on demand". Otherwise I have no idea what is going on here. Can anonye explain?

Thanks
Oliver

André R.

Thursday 07 August 2008 7:00:48 am

From the var_dump ypu can see that this is a object of type eZInformationCollectionAttribute, not a array.

The clean way to get the variable would be (this is supported by all classes that are meant to be used inside a template):

$dataInt = $attribute->attribute('data_int');

Or the regular way:

$dataInt = $attribute->DataInt;

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

Oliver Frommel

Thursday 07 August 2008 8:36:29 am

This is great, thanks.

Maybe you can also explain how I get the description of the options that are found in the content object instance belonging to the information collection? The only thing I could find was a text attribute filled with an XML fragment like this:

                // <ezoption>
                // <name>company_size</name>
                // <options>
                //    <option id="0"
                //           additional_price="0">0-10</option>
                //   <option id="1"
                //          additional_price="0">10-20</option>
                //     <option id="2"
                //          additional_price="0">20-30</option>
                //    <option id="3"
                //            additional_price="0">30-40</option>
                //   </options>
                // </ezoption>
                //

I was able to parse it but it is a bit cumbersome. I thought there was a way just looking the description up via an (associative) array?

Thanks
Oliver

André R.

Thursday 07 August 2008 9:13:55 am

Some template code for selection attribute that might help:

{def $portion_array            = $node.data_map.portions_type.content
     $portion_type             = ''}
{foreach $node.data_map.portions_type.class_content.options as $option}
    {if $portion_array|contains( $option.identifier )}{set $portion_type = $option.name|wash(xhtml)}{/if}
{/foreach}

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

Main resources:

Total runtime0.8249 sec
Peak memory usage4,096.0000 KB
Database Queries60

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 587.9141152.6250
Module start 'layout' 0.00540.0029 740.539139.4453
Module start 'content' 0.00830.8149 779.9844560.0469
Module end 'content' 0.82320.0016 1,340.031316.1641
Script end 0.8249  1,356.1953 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00320.3876160.0002
Check MTime0.00140.1749160.0001
Mysql Total
Database connection0.00080.092610.0008
Mysqli_queries0.770093.3426600.0128
Looping result0.00090.1077580.0000
Template Total0.792496.120.3962
Template load0.00220.269620.0011
Template processing0.790295.790720.3951
Template load and register function0.00030.030310.0003
states
state_id_array0.00150.177710.0015
state_identifier_array0.00090.112020.0005
Override
Cache load0.00190.2254310.0001
Sytem overhead
Fetch class attribute can translate value0.00080.093820.0004
Fetch class attribute name0.00240.285350.0005
XML
Image XML parsing0.00060.074220.0003
class_abstraction
Instantiating content class attribute0.00000.001860.0000
General
dbfile0.00090.1070170.0001
String conversion0.00000.001040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
11content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
7content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 28
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs