Reputation: 1430
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
Reputation: 347
In addition to the accepted answers see benchmarks:
Tested for 750 products
$collection->getData()
$collection->getSize()
$collection->count()
or sizeof($collection)
So you should go with getSize()
.
Upvotes: 3
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
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
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
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
Reputation: 2334
You can easily just do an if statement like so:
if (!$datas->getData() || empty($datas->getData())) {
// do things
}
Upvotes: 4
Reputation: 339
you can use;
$collection = Mage::getModel('zzz/zzz')->getCollection();
var_dump($collection->count());
Upvotes: 1