Tuesday, October 26, 2010

Check if a Zend_Db_Table_Rowset is empty

When retrieving data from the database using Zend Framework (using find or fetchAll functions) you will be presented with a Zend_Db_Table_Rowset. To check if it is empty it would seem logical to use the empty function like when checking an array. This will not work.

To check if a Zend Rowset (Zend_Db_Table_Rowset) is empty you need to use the count function. e.g.:
if(count($result) > 0) {
//Do something
}

Or using the Rowsets own count function (which does not iterate through all the elements apparently):
if($result->count() > 0) {
//Do something
}


See http://www.mail-archive.com/fw-general@lists.zend.com/msg25411.html for a discussion on the topic.

No comments:

Post a Comment