I’m working hard to get the xPDO documentation up to date and more complete. In the meantime, I wanted to introduce everyone to the latest feature additions to xPDO 1.0 alpha (currently only available from SVN; I’ll update the download packages later today) as of revision 27.
xPDOQuery --
Ok, so I decided some SQL abstraction was necessary.
Based on feedback from a few early adopters of xPDO, I have added a significant new extension to the xPDO arsenal. Previously, in order to do more complex queries on the objects, it was necessary to write the SQL to do so. You could wrap it with an xPDOCriteria object to pass it into the xPDO object handling methods along with some bindings. But all too common are slight variations of the scaffolding SQL that is automatically handled by the object layer, and we needed a way to get record counts, apply limits, order by clauses, additional where conditions, etc. Enter xPDOQuery.
xPDOQuery extends the xPDOCriteria class, allowing programmatic control over the generated SQL, via a logical API. You can still manually wrap SQL with xPDOCriteria, but compare the two approaches:
Old way, using xPDOCriteria:
<?php
$webUserTable= $xpdo->getTableName('modWebUser');
$sql= "SELECT * FROM {$webUserTable} WHERE `id` = :user_id LIMIT 1";
$bindings= array(
':user_id' => array ('value' => $userid, 'type' => PDO_PARAM_INT)
);
$criteria= new xPDOCriteria($this->xpdo, $sql, $bindings);
$user= $xpdo->getObject('modWebUser', $criteria);
Same query, new way, using xPDOQuery (via $xpdo->newQuery()):
<?php
$criteria= $xpdo->newQuery('modWebUser', $userid);
$criteria->limit(1);
$user= $xpdo->getObject('modWebUser', $criteria);
and in PHP 5, you can even use the shorthand...
<?php
$criteria= $xpdo->newQuery('modWebUser')->where($userid)->limit(1);
$user= $xpdo->getObject('modWebUser', $criteria);