PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Structures » Structures_DataGrid » Bug #5076

Add PDO Driver for DataGrid

Details

Request #5076Add PDO Driver for DataGrid
Submitted2005-08-12 10:00 UTC
Fromferrante dot enriques at pleiade dot it
Assignedwiesemann
StatusClosed
PackageStructures_DataGrid
PHP Version5.1.0
OSAll
Roadmaps(Not assigned)

Comments

[2005-08-12 10:00 UTC] ferrante dot enriques at pleiade dot it

Description:
------------
Description, source code and example here:
http://www.pleiade.it/datagrid/

[2005-10-06 19:24 UTC] olivierg at php dot net

Hi Ferrante,

Thank you for this contribution to Structures_DataGrid. I will try and integrate your new PDO driver in the next days.

However, I see that you made two versions of DataSource.php. Could you try and make a single version that work in both php4 and php5 ?

[2005-10-07 09:23 UTC] ferrante dot enriques at pleiade dot it

php4 version of datasource.php works well with both php4 and php5. In the php5 version, it only checks if the class exists before instantiating it, see lines 180-187, using Reflection API that are new to php. That checking is indipendent of the specific datasource and could be used in a brand new version of Structures_datagrid that is designed specifically for php5. So don't mind and use php4 version safely for both php4 and php5 (but remember that PDO is extension of php5 only...)
Real issues are in the PDO driver itself: I found no way to have a better numrows than counting all the rows of the recordset (in PDO afaik there is no included numrows function...)

[2005-10-07 10:38 UTC] olivierg at php dot net

Okay for DataSource.php

I saw your PDOStatement::fetchAll() call within the DataSource_PDO::count() method. That is a problem... Structures_DataGrid should not perform such heavy queries. Just imagine a little 10k rows table...

Additionnaly I see that it's not possible to use PDO::query() to issue a count() like sql query. This would count all of the table rows, where the user may only have selected a subset of them, using a where clause.

I believe that this is a serious PDO limitation where DB_DataObject has a smart count() method.

As a workaround I propose to add an option to the DataSource_PDO driver.

One would for example do :
$result= $pdo->query(
"SELECT name, age, sex
FROM users
WHERE sex = 'male'"
);
$countResult = $pdo->query(
"SELECT count(*) FROM users
WHERE sex='male'"
);
$numRows = $countResult->fetchColumn();

$options = array ('numRows' => $numRows);

$dg->bind($result, $options);
$dg->render();

This 'numRows' option would be optionnal. If not provided, the PDO driver would call PDO::fetchAll(). _But_ the _documentation_ should clearly state that this 'numRows' option is necessary for optimization.

What do you think about this idea ? If it's ok with you, is it possible for you to send me an updated version ?

As a general guideline, please send patches (cvs diff -u) instead of .phps files.

Thanks

[2005-10-07 12:01 UTC] ferrante dot enriques at pleiade dot it

I believe that lack of numrow for PDO is a big limitation too. I stopped using PDO on my projects because of that.

FetchAll is not called. Or better *should not* be called (both using what i've done or your proposal)
The code in PDO.php (line 104+) is:

while ($record = $this->_result->fetch(PDO_FETCH_ASSOC)) {
$recordSet[] = $record;
}
$this->_rowNum = count($recordSet);
if($this->_rowNum> 0 ){
...render fields...
}

This mean I count records matched by the exact query. And no need to perform additional queries, just added a private var _rowNum.

I don't remember why I put a call to fetchAll (line 132):

if ($this->_rowNum == null) {
$this->_rowNum = count($this->_result->fetchAll());
}

Should be tested. I apoligise for that but I discontinued the usage of PDO and related classes.

[2006-02-12 13:37 UTC] ferrante dot enriques at pleiade dot dotit

PDO is meant for data access and not for database abstraction. Not having a good row count function is just consequence of this difference in scope.

IMO PDO should be used in Datagrid (or any other package that deal with data abstraction) not directly. i.e. DataGrid should use MDB2, or other, for abstraction and MDB2 could use PDO for data access.

For this reason I suggest not to include PDO driver for DataGrid anymore.
Ok I started this topic...but my understanding of PDO was, and actually is, at an early stage.
Any thoughts?