Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4544796
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:43:20+00:00 2026-05-21T15:43:20+00:00

I am running a loop through a product collection like the following $productCollection =

  • 0

I am running a loop through a product collection like the following

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('small_image')
    ->addAttributeToSelect('thumbnail')
    ->addAttributeToSelect('image')
    ->addAttributeToSelect('sku');

foreach($productCollection as $product){
    $product->setSmallImage($product->getImage());
    $product->setThumbnail($product->getImage());
    $product->save();
}

I am setting the other image types to the same as the base image because the client forgot to set those in the import sheet. For some reason when doing this it is setting the visibility of all products to Catalog, Search. A lot of my products are suppose to be “Not Visible Individually”, so of course this messes stuff up.

Any Idea why this value would change, and are there other values that might change based on how I looped through the product collection?

I think it is because i removed the addAttributeToSelect(*) and did it specific to the two attributes but when trying to do the product collection over 18k products it was crapping out

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:43:20+00:00Added an answer on May 21, 2026 at 3:43 pm

    Ok so this is what I found. When saving an object in Magento, it will call the normal Model abstract save method, this then calls $this->_getResource()->save($this); which will call the entity abstract save method, which eventually gets to /app/code/core/Mage/Eav/Model/Entity/Abstract.php and its _beforeSave() method which calls the walkAttributes($partMethod, array $args=array()) method.

    A long the way all the attributes are loaded the the Resource object of the entity. The walkAttributes method will go through all attributes associated to the entity and calls this method call_user_func_array(array($instance, $method), $args); and in my case it is calling the beforeSave method for an attribute, as you can see here

    /app/code/core/Mage/Eav/Model/Entity/Attribute/Backend/Abstract.php
    public function beforeSave($object)
        {
            $attrCode = $this->getAttribute()->getAttributeCode();
            if (!$object->hasData($attrCode) && $this->getDefaultValue()) {
                $object->setData($attrCode, $this->getDefaultValue());
            }
        }
    

    So any attribute that does not have any data and has a default value, that default value is applied to the object, in sense overwriting my data that is really set just not loaded to my object. I am guessing the best way to loop over thousands of products is to set the pageSize to a number that your server can handle loading and then you can set addAttributeToSelect(‘*’). However I am not aware of the best way to loop over this correctly, so for me I might just load the product in the for loop and not set the attribute to select all.

    Here is my attempt at looping over the whole collection, please post any improvements

    $productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->setPageSize(200);
    for ($i = 1; $i <= $productCollection->getLastPageNumber(); $i++) {
        if ($productCollection->isLoaded()) {
            $productCollection->clear();
            $productCollection->setPage($i);
            $productCollection->setPageSize(200);
        }
        foreach ($productCollection as $product) {
            echo $product->getId() . "\n\n";
        }
        echo $i . "\n\n";
    }
    

    I would like to still hear any other input on this as to whether this is the right thinking or explanation of the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running the following code. The code does not loop through all the
I'm looking for an answer to this problem: I'm running through a loop testing
I am currently returning an array and running a for loop through it. The
Say you want to loop through 3 elements like this: for(int i=0; i<3; i++)
I'm using the following code to loop through a directory to print out the
I want to loop through my running VM's and return only what is between
I would like to set up a simple batch file that would loop through
I have the following code that I'm attempting to simply loop through an entity
I am trying to set up a running loop, and to do so I've
I am running a loop, and I am trying to create a variable each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.