2

I am trying to get all stores on a Magento shop. By all stores, I mean all stores from all websites. I wrote this code and it works, but I'm a little concerned about the complexity of the nested foreach loop. Please take a look at it and advise me if you think I can do something different.

public function getAllStoresCustom(){

    $all_stores = array();

        foreach (Mage::app()->getWebsites() as $website) {
            foreach ($website->getGroups() as $group) {
                $all_stores [] = $group->getStores();      
            }
        }

    return $all_stores;
}

I've only found these functions in Magento, so I think I had to use those and this seemed the only combination that worked.

Thanks a lot

2 Answers2

3

Try this:

$allStores = Mage::getModel('core/store')->getCollection();

Then loop through $allStores when needed

foreach ($allStores as $store) {
   //do something with $store
}

Notice: You will get a store with id 0. That is the admin store view. If you want all stores without the admin store view use this:

$allStores = Mage::getModel('core/store')->getCollection()->setWithoutDefaultFilter()
Marius
  • 15,148
  • 9
  • 56
  • 76
  • Thanks, that did work and it eliminated my loops! I suppose this 'collection' is connected to the Magento EAV model in a way? I'll probably have to read Alan Storm's articles about that. – user3284189 Feb 07 '14 at 14:51
  • The `stores` objects are not EAV. a collection is basically a list of objects. – Marius Feb 07 '14 at 14:53
1

foreach() is an incredibly efficient PHP function so you can bet that it is not going to be your slowdown. If you are looking to optimize something then look into the code for the getStores() and getGroups() functions because those are being called within the iterations whereas getWebsites() gets called only once.

If you want more guidance then please feel free to update your question with the contents of those functions.

You may also want to try https://codereview.stackexchange.com/ for more experienced opinions especially since you don't have any specific programming issue/error =)

Community
  • 1
  • 1
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • every loop can be a problem if used unnecessarily on a big dataset ;). But in this case it looks like it wouldn't be a problem, although there may be a more direct function by Mage – cypherabe Feb 07 '14 at 14:35
  • @cypherabe you are absolutely correct but without more context from OP we are all just guessing =) – MonkeyZeus Feb 07 '14 at 14:37
  • Ok, I was thinking about nested foreach loops in general. As it turned out, @Marius suggested a better Magento function which eliminated the loops completely. But, are there some general rules that help to avoid nested loops? Thanks – user3284189 Feb 07 '14 at 14:53
  • If you are asking about something like "efficient PHP looping" then I am sure you will find plenty of advice through Google. Here a few I found: http://www.phpdreams.com/blog-posts/best-practice-array-loops.html http://stackoverflow.com/questions/12847502/for-loop-vs-while-loop-vs-foreach-loop-php http://stackoverflow.com/questions/1742215/php-foreach-loops-and-ressources http://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach and make sure to look into something called `pass by reference` – MonkeyZeus Feb 07 '14 at 17:27
  • To answer your question though, you can generally avoid nested PHP loops by properly querying your database with JOINs. I helped a guy perform a JOIN in **[this SO question](http://stackoverflow.com/a/21561505/2191572)** if you are interested. – MonkeyZeus Feb 07 '14 at 17:28