Can't load all products in Indexcontroller
-
16-10-2019 - |
문제
I have a simple Indexcontroller.
When I use this:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
echo count($collection);
It doesn't show all the product (just a few), what could the reason for this be? I would like to load all products, doesn't matter what the status is.
UPDATE:
Thanks to Alan Storm I found the answer. In using: echo (string) $collection->getSelect();
I saw: (_table_name.store_id = 5)
By setting it to storeId 0 ('admin' so to say) I was able to see all the product.
Sounds logical... as always :-)
해결책
Unfortunately, there's too many reasons to list (or to even know) why your product collection may not be returning all the products you think it should. It may be a bug, may be an extension you installed, or may be you have a misunderstanding as to what's returned in a default collection.
Fortunately, this is a relatively simple thing to debug yourself. You can take a peek at the SELECT
statement used by the collection to populate its initial objects.
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
echo (string) $collection->getSelect();
With that SQL in hand, it should be pretty easy to tell why Magento isn't returning the objects you think it should.
다른 팁
There is more than one way to get the count of items in a collection. Unfortunately the PHP internal count
function is probably not the most performant way.
Here are two others to try:
echo $collection->count();
echo $collection->getSize();
What I suspect is happening is either your collection isn't as deep as you think it is; there is a memory issue with the PHP count
function (iterates the collection as an array); or count
isn't aware of how many items there actually are due to lazy loading, where the collection isn't actually being loaded up at the time you're asking for its count.