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

Issue Type: Improvement Created: 2006-06-20T00:43:09.000+0000 Last Updated: 2007-07-05T14:43:08.000+0000 Status: Resolved Fix version(s): - 0.9.1 (23/Mar/07)

Reporter: Zend Framework (zend_framework) Assignee: Bill Karwin (bkarwin) Tags: - Zend_Db_Table

Related issues: Attachments:


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:

<pre class="highlight">
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<br></br>\n";
} else{
    echo "User does not exist in the database<br></br>\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();



Posted by Matt Schmidt (sloat) on 2006-06-22T10:05:27.000+0000

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.

Posted by Emil Hesslow (wizkid) on 2006-11-04T20:12:13.000+0000

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

<pre class="highlight">
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?

Posted by Bill Karwin (bkarwin) on 2006-11-13T15:22:38.000+0000

Changing fix version to 0.8.0.

Posted by Bill Karwin (bkarwin) on 2007-01-05T17:06:26.000+0000

Recategorize as Zend_Db_Table component.

Posted by Bill Karwin (bkarwin) on 2007-02-11T11:59:24.000+0000

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:

<pre class="highlight">
$row = $Table_User->find(1);
if ( !$row->exists() ) {
    //no result


Posted by Bill Karwin (bkarwin) on 2007-03-15T23:36:43.000+0000

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.

Posted by Emil Hesslow (wizkid) on 2007-03-18T17:45:43.000+0000

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.

Posted by Bill Karwin (bkarwin) on 2007-03-19T12:06:02.000+0000

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).

Posted by Bill Karwin (bkarwin) on 2007-03-19T12:06:23.000+0000

Reopening issue for previously mentioned edits.

Posted by Bill Karwin (bkarwin) on 2007-03-19T13:51:43.000+0000

Modifying summary. The methods return NULL, not FALSE.

Posted by Bill Karwin (bkarwin) on 2007-03-19T13:52:03.000+0000

Updating fix version to 0.9.1.

Posted by Bill Karwin (bkarwin) on 2007-03-19T19:07:38.000+0000

Committed in revision 4128.

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.