ZF-45: Zend_Db_Table::fetchRow() should return NULL for no resulting rows


My first problem is very simple, I can´t identify if fetchRow() found a row in the database or not, it does always return a Zend_Db_Table_Row instance. In the constructor of Zend_Db_Table_Row the _data property is filled with blanks if no-data is passed.

My first problem is very simple: I can´t tell if fetchRow() actually found a row in the database since it always returns a Zend_Db_Table_Row instance. The protected _data property of Zend_Db_Table_Row is filled with blanks by the constructor. Zend_Db_Adapter_Pdo_Mysql::fetchRow() return FALSE, though.

I would like to write the following code:

class User extends Zend_Db_Table {}

$table = new RoundTable();
$db = $table->getAdapter();

$row = $user->fetchRow( $db->quoteInto('id = ?', 5) );

if($row === false) {
    echo "User was found in the database
\n"; } else{ echo "User does not exist in the database
\n"; }

{quote} 05/09/06 18:25:32: Modified by Jens Kleikamp

Sorry for the wired ticket, it comes from the ml, where I made such a scruffy post by an oversight. 05/09/06 18:26:48: Modified by Jens Kleikamp

-$table = new RoundTable(); +$table = new User();



Checking the data in Zend_Db_Table::fetch(Row|All) would work. Having Zend_Db_Table_Row::__construct throw an exception would be unnecessary, IMHO.

Wouldn't it be smarter to change the function to:

public function fetchRow($where = null, $order = null)
    $data = $this->_fetch('Row', $where, $order, 1);
    if ($data === false)
        return null;

    return new Zend_Db_Table_Row(array(
        'db'    => $this->_db,
        'table' => $this,
        'data'  => $data,

This would mean that you get null returned if there is no row. Because if one want a new row you just use newRow(). Or have I misunderstood something?

Changing fix version to 0.8.0.

Recategorize as Zend_Db_Table component.

Nick Thornley writes (2007-02-11):

I'm just wondering what other people are doing to check if a row object has been populated, as my first thought is that an exists() method would be a useful, just as see there is in Zend_Db_Table_Rowset?

for example, if you have just returned a Row object from a Zend_Db_table object it would be nice to do:

$row = $Table_User->find(1);
if ( !$row->exists() ) {
    //no result


As of revision 3995, the find() method always returns an object of type Zend_Db_Table_Rowset_Abstract.

Therefore you can use an exists() method to find out if the find() method returned any rows.

Can I ask why it can't just return null? I must say that I haven't used ZF that much but when I have there is very rare that I need a object row if find() don't find anything. And if I do need a new object row I can just call fetchNew(). Probably in more then 90% of the cases I have no need for the empty row object.

I my self have used the code I posted before that just returns null if there isn't exist any row object. Very easy to check and I don't get any empty row object that I don't use.

Yes, I will make the following changes:

Zend_Db_Table_Abstract::fetchRow() returns a Zend_Db_Table_Row_Abstract object, or else null if no rows are found.

Zend_Db_Table_Abstract::find() and fetchAll() each return a Zend_Db_Table_Rowset_Abstract object. This object has a method called exists() for testing if the rowset is empty.

Zend_Db_Table_Row_Abstract::findDependentRowset() returns a Zend_Db_Table_Rowset_Abstract object.

Zend_Db_Table_Row_Abstract::findParentRow() returns a Zend_Db_Table_Row_Abstract object, or else null if no rows are found (though the latter case should never happen in a database with referential integrity).

Reopening issue for previously mentioned edits.

Modifying summary. The methods return NULL, not FALSE.

Updating fix version to 0.9.1.

Committed in revision 4128.