Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.9.3

Request #8061 Get data as an array
Submitted: 2006-06-28 19:54 UTC
From: derernst Assigned: olivierg
Status: Closed Package: Structures_DataGrid (version 0.7.1)
PHP Version: 4.3.9 OS:
Roadmaps: (Not assigned)    

 [2006-06-28 19:54 UTC] derernst (Markus Ernst)
Description: ------------ In order to use datasource drivers for data import it would be handy to get the recordset after having bind()ed the datasource. For consistency with &getRenderer() I suggest a &getDataSource() method for Structures_DataGrid to access datasource object in order to fetch() the data. Further, for processing the fetched data I suggest a getLabels() method for Structures_DataGrid_DataSource. I hope you forgive me for just pasting the functions that work for me here - they are only suggestions, it is little code, and before contributing patches of suitable quality I think I have to invest some more time in learning about it. Test script: --------------- // Method for DataGrid.php: function &getDataSource() { return isset($this->_dataSource) ? $this->_dataSource : false; } // Method for DataSource.php: function getLabels() { $labels = $this->_options['labels']; if (!$labels && $this->_options['fields']) { foreach ($this->_options['fields'] as $field) { $labels[$field] = $field; } } return $labels; }


 [2006-06-28 20:02 UTC] derernst
I am sorry I am tired and will stop posting for today ;-) - I forgot to add the use example: $ds =& $datagrid->getDataSource(); $data = $ds->fetch(); $cols = $ds->getLabels(); for ($i=0; $i<count($data); $i++) { foreach ($cols as $field => $label) { echo $label.': '.$data[$i][$field].'<br>'; } }
 [2006-06-29 18:48 UTC] olivierg at php dot net (Olivier Guilyardi)
I've added getDataSource(), but a slightly different version, because you can't return by reference from an expression : Anyway, I've also made datasourceFactory() public, because I think that it better fits your need. It even is static, look : $datasource = Structures_DataGrid::datasourceFactory($xml); - it provides datasource container detection - it saves memory/cpu as compared to getDataSource() : - no need to instantiate the DataGrid object - no need to call bind() and fill the DataGrid::_recordSet; property. Actually, in the past, this method was public and named DataSource::create(). Concerning getLabels() : why don't you use DataSource::getColumns() ? I understand that you need to learn more about SDG and contributing patches. No problem. Actually we currently need to improve the documentation. Maybe that you could help us with that, if you like ? I believe that correcting and writing documentation would help you understanding SDG, and that would involve learning about XML (DocBook) and submitting patches. More about contributing docs :
 [2006-06-29 19:01 UTC] derernst
Thank you, I will be glad to look at this. I was actually just posting that my suggestions are included in the updated patch available at I also propose a getType() method for the datasource driver, as it would help to set the correct options and things like that. The getLabels() is there because getColumns() gives me the column objects, but they don't provide a possibility to retrieve their name and field properties publicly. I think that generally it would be handy to have public methods to get the basic info and contents from every object.
 [2006-06-29 19:22 UTC] olivierg at php dot net (Olivier Guilyardi)
Okay for getType() but you're method does not return a type. This is a type : DATAGRID_SOURCE_XML (constant, integer) This is not a type : "XML" (string) I don't really like the DataSource::getLabels() method, because it's conceptually wrong IMO (related to bug #7710). What you need is Column::getColumnName(). I quite agree with you on this : "I think that generally it would be handy to have public methods to get the basic info and contents from every object." Especially on the Column object, which lacks many setters and getters.
 [2006-06-29 20:26 UTC] wiesemann (Mark Wiesemann)
I agree here, getLabels() is the wrong way. I'll take care of adding the missing getters/setters in the column class (already thought of it, but forgot it).
 [2006-07-03 20:44 UTC] wiesemann (Mark Wiesemann)
Okay, we have now the getters and setters with meaningful names. Is there anything else before we can close this request?
 [2006-07-04 08:44 UTC] derernst
I changed my application to work with the current CVS version of the package, and I can do everything I need now. (Anyway, I chose to do source type detection separately, as I need the CVS and delimiter detection functionality. IMO this and some kind of DataGrid::getSourceType() or DataSource::getType() will be needed to make automatic source detection perfect - but this is not subject to this request, though.) I think this request can be closed, except one small error I found regarding the staticness of DataGrid::dataSourceFactory(): RCS file: /repository/pear/Structures_DataGrid/DataGrid.php,v retrieving revision 1.94 diff -u -r1.94 DataGrid.php --- DataGrid.php 3 Jul 2006 19:07:52 -0000 1.94 +++ DataGrid.php 4 Jul 2006 08:22:53 -0000 @@ -400,7 +400,7 @@ $className = "Structures_DataGrid_DataSource_$type"; - if (PEAR::isError($driver =& $this->loadDriver($className))) { + if (PEAR::isError($driver =& Structures_DataGrid::loadDriver($className))) { return $driver; } Thanks, you guys do really great work!
 [2006-07-04 16:42 UTC] olivierg at php dot net (Olivier Guilyardi)
Thank you ! I've fixed this non-static call in CVS. This bug is closed.