Shadowbob
Shadowbob

Reputation: 1430

Detect if collection contains data

It may be a simple question, but I can't find the answer. How can I know if my Collection has no data ?

I do $datas = Mage::getModel('zzz/zzz')->getCollection() if I do a $datas->getData() it returns an empty array, but how do I know if my collection has no data without doing foreach or getData ?

Upvotes: 11

Views: 17551

Answers (7)

sv3n
sv3n

Reputation: 347

In addition to the accepted answers see benchmarks:

Tested for 750 products

$collection->getData()

  • Total Incl. Wall Time (microsec): 67,567 microsecs
  • Total Incl. CPU (microsecs): 67,599 microsecs
  • Total Incl. MemUse (bytes): 11,719,168 bytes
  • Total Incl. PeakMemUse (bytes): 11,648,152 bytes
  • Number of Function Calls: 1,047

$collection->getSize()

  • Total Incl. Wall Time (microsec): 6,371 microsecs
  • Total Incl. CPU (microsecs): 4,402 microsecs
  • Total Incl. MemUse (bytes): 140,816 bytes
  • Total Incl. PeakMemUse (bytes): 96,000 bytes
  • Number of Function Calls: 191

$collection->count() or sizeof($collection)

  • Total Incl. Wall Time (microsec): 2,130,568 microsecs
  • Total Incl. CPU (microsecs): 2,080,617 microsecs
  • Total Incl. MemUse (bytes): 12,899,872 bytes
  • Total Incl. PeakMemUse (bytes): 13,002,256 bytes
  • Number of Function Calls: 101,073

So you should go with getSize().


Form: https://magento.stackexchange.com/questions/179028/how-to-check-if-a-collection-has-items/179035#179035

Upvotes: 3

Prithweema
Prithweema

Reputation: 141

Suppose the product collection is $pro_collection

Now apply the following code..

<?php 
if(isset($pro_collection) && count($pro_collection) > 0) {
    /* Your code here */
}
?>

Upvotes: 0

Jonathan Hussey
Jonathan Hussey

Reputation: 1044

Running a simple, standard PHP count() on the collection is fine here. As long as you have properly filtered your collection, which you should always have done before getting to the point of counting it calling the ->count() method on a collection is fine also. As soon as you manipulate the collection in any way, it will load regardless of the method you use, so a standard PHP count(), calling the ->count() method on the object, running through the collection with a foreach() will all load the collection in the same way as as load(), in fact if you trace the load() method back, you will see it actually runs a standard PHP foreach() to load collection data.

So it doesn't matter how you do it, you still can't count your collection until you know how many results have been returned from the database, so the method above is fine, but does mean extra DB calls, first to count, then to load. A better method is just to ensure that you make your SELECT statements as specific as possible by narrowing them with WHERE clauses and so on. If you pull the select object from a collection you have access to all of the Zend_Db_Select methods shown here, i.e.

$collection->getSelect()->where('......... = ?', $var);

Upvotes: 1

Slayer Birden
Slayer Birden

Reputation: 3694

You should avoid using count or your Collections. Here's why:

the Mage_Core_Model_Resource_Db_Collection_Abstract (Collection Model that is inherited by almost all Magento Collections) does not have count() defined, so using count on your Collection you'll most likely end up with Varien_Data_Collection::count() which is very bad option, since it does a collection load() and then counts the loaded objects:

/**
 * Retireve count of collection loaded items
 *
 * @return int
 */
public function count()
{
    $this->load();
    return count($this->_items);
}

Having a large collection (especially EAV collection) will make result in loading ALL of your Collection data - this can take a lot of time.

Instead you should use Varien_Data_Collection_Db::getSize() method, which will run the SQL query to get count only, much more optimized compared to retrieving all kind of data for Collection load:

/**
 * Get collection size
 *
 * @return int
 */
public function getSize()
{
    if (is_null($this->_totalRecords)) {
        $sql = $this->getSelectCountSql();
        $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
    }
    return intval($this->_totalRecords);
}

In addition to that, after load collection can not be modified in any way. For example you won't be able to apply additional filters of change sort order at any point after using count().

So correct answer should be:

$collection = Mage::getModel('zzz/zzz')->getCollection();
var_dump($collection->getSize());

Upvotes: 22

Oscprofessionals
Oscprofessionals

Reputation: 2174

/** * Retrieve collection all items count * * @return int */ $collection = Mage::getModel('aaa/bbb')->getCollection()->getSize();

This is the code that's used in pagination etc and is recommended.

where /** * Retireve count of collection loaded items * * @return int */ public function count()

will be useful to check for loaded items data.

Upvotes: 2

1000Nettles
1000Nettles

Reputation: 2334

You can easily just do an if statement like so:

if (!$datas->getData() || empty($datas->getData())) {
     // do things
}

Upvotes: 4

duzenz
duzenz

Reputation: 339

you can use;

$collection = Mage::getModel('zzz/zzz')->getCollection();
var_dump($collection->count());

Upvotes: 1

Related Questions