For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > June 2006 > Re: [PEAR] Structures_Datagrid_Renderer_XML: Use column names as









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PEAR] Structures_Datagrid_Renderer_XML: Use column names as
Olivier Guilyardi

2006-06-26, 7:01 pm

Hi Markus,

Markus Ernst wrote:
> After the last announcements I tried Structures_Datagrid, which is a
> really useful tool! Anyway I found no way to make the XML renderer use
> the column name for the field attribute, instead of the field name. I
> see that it is normally useful to have the field name, but in my case
> where I have fields with an ID, whose column names are language
> specific, this leads to an inconsistent behaviour, as this test case shows:


I'm glad you like SDG :-)

> <code>
> $fields = array(2 => 'Last name', 1 => 'First name', 'lang' => 'Language');
> $data = array(
> array(2 => 'Smith', 1 => 'John', 'lang' => 'English'),
> array(2 => 'Schmid', 1 => 'Hans', 'lang' => 'German'),
> array(2 => 'Dupont', 1 => 'Jean', 'lang' => 'French')
> );
> // Get datagrid object, bind data
> require_once 'Structures/DataGrid.php';
> $datagrid =& new Structures_DataGrid();
> $datagrid->bind($data);
> // Set columns
> foreach ($fields as $field_id => $field_name) {
> $datagrid->addColumn(new Structures_DataGrid_Column($field_name,
> $field_id));
> }
> // Output XML
> $datagrid->setRenderer(DATAGRID_RENDER_XML);
> $renderer =& $datagrid->getRenderer();
> $renderer->setOptions(array('outerTag' => 'UserList', 'rowTag' => 'User'));
> $datagrid->render();
> exit;
> </code>
>
> This outputs this code, which is considered as not wellformed by Firefox:
>
> <?xml version="1.0"?>
> <UserList>
> <User>
> <2>Smith</2>
> <1>John</1>
> <lang>English</lang>
> </User>
> <User>
> <2>Schmid</2>
> <1>Hans</1>
> <lang>German</lang>
> </User>
> <User>
> <2>Dupont</2>
> <1>Jean</1>
> <lang>French</lang>
> </User>
> </UserList>


Apart from the fact that this XML output is not well-formed, that's the expected
behaviour. The second argument to the SDG_Column constructor is the $fieldName,
as used for example in SQL queries, etc... This is mapped to the records arrays
keys, as in : array(2 => 'Smith', 1 => 'John', 'lang' => 'English').

> While using other renderers such as XLS or CSV create outputs with
> column names:
>
> | Last name | First name | Language |
> | Smith | John | English |
> | Schmid | Hans | Deutsch |
> | Dupont | Jean | French |


The column names (first SDG_Column argument, $columnName) is a label. It is not
meant to identify a field of data but to present that field to the user.

Although that might be arguable for the CSV renderer, it is normal for the XLS
renderer to output the labels (Column names), since its output is expected to be
read by the end-user.

But for the XML renderer, we name the tags after the field names (what you
called $field_id), that's more coherent with the nature of XML, which is likely
to get parsed by some external programs.

> I think for consistency reasons it should be possible to make the column
> name appear in the XML structure (the output should IMO be forced to be
> wellformed anyway, making the package a useful tool for people not
> familiar with the specifications of the formats outputted). I don't know
> very much about XML - I guess some output like the following would be good?


Structures_DataGrid can not replace basic knowledge of its input and output
formats. With very limited XML knowledge one can see why such input is likely to
produce bad XML.

If we check for every sort of input that may cause the resulting XML not to be
well-formed, it might result in SDG getting bloated/heavy. But I'm not sure
about this point ; feel free to post a bug report about this issue.

> <User>
> <field0 id='2' name='Last name'>Smith</field0>
> <field1 id='1' name='First name'>John</field1>
> <field2 id='lang' name='Language'>English</field2>
> </User>


SDG_Column's field names ($fieldName) are very much like column fields in
databases. In this regard, it is coherent to use them as tag names.

But I agree a syntax like <item id="lang" name="Language"> English </item> could
be an alternative.

You might want to post a feature request about this.

IMO it should only be an "alternative" though, since it is bloated as compared
to : <lang> English </lang>

Btw, you can easily achieve maximum flexibility by writing a simple custom-made
renderer, subclassing Structures_DataGrid_Renderer, and implementing one or two
methods : buildRow() and (if you want to buffer output) flatten().

Thanks for your feedback

--
Olivier
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com