Magento Forum

   
Loading Magento Store Names into select box
 
gokuljeyapaul
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2012-03-11
 

Hi,

I am creating a module to basically do a CRUD operation on a table(custom table) that maps magento store to a boolean flag. The mysql table has three columns basically - id(pk), store_id(fk to core_store store_id), boolean_value.

For this CRUD, I want to show store names in a “select” in the screen and store the store_id into mysql table. How do I add a select field that backs a core_store model in the UI?

I presently have the following,

$fieldset->addField(’store_id’, ‘text’, array(
‘label’ => Mage::helper(’samplemodule’)->__(’Store Name’),
‘class’ => ‘required-entry’,
‘required’ => true,
‘name’ => ‘store_id’
‘values’ =>
));

What should go in the values in this case?

I managed to get the store models as below,

$storeCollection = Mage::getModel(’core/store’)->getCollection();

How do I use it or form a select out of it?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello
Try it out like this:

$fieldset->addField('store_id''select', array(
                
'name'      => 'store_id',
                
'label'     => Mage::helper('samplemodule')->__('Store View'),
                
'title'     => Mage::helper('samplemodule')->__('Store View'),
                
'required'  => true,
                
'values'    => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(truefalse),
            ));
If you change the second parameter for getStoreValuesForForm to ‘true’ you will get an additional option ‘All store views’.

If you also have a grid view for your custom module, you can add the store view column like this:

$this->addColumn('store_id', array(
                
'header'        => Mage::helper('samplemodule')->__('Store View'),
                
'index'         => 'store_id',
                
'type'          => 'store',
                
'store_all'     => false,
                
'store_view'    => true,
                
'sortable'      => false,
            ));
Again, if you change the value for ‘show_all’ to true you will get the ‘All store views’ option.
Let me know how it turns out.
Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
gokuljeyapaul
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2012-03-11
 

Thanks a bunch, it works like charm grin

Are there set of Magento best practices/optimized ways documented somewhere? Forgive my limited knowledge, I am a beginner to both PHP and Magento

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello again.
There are some tutorials and a bit of documentation here, but it’s a bit general.
For specific stuff, the forum and a few blogs (and good old Google) are the best information sources.
The chances are that someone had the same issue before you did (but not always).
For issues like the one you posted here the best source is the Core code itself.
You can check the grids and forms from the core modules for similar fields and grids.

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
gokuljeyapaul
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2012-03-11
 

Thanks Marius..

I have few questions related to this itself, hope some one could help me with it..

- Can I have an observer for “store” load event? I referred this list Magento events Cheat Sheet and could not find one...I tried the following and it did not work,

<events>
               <
core_store_load_after> <!-- event -->
                    <
observers> <!-- list of observers -->
                        <
sample_module><!-- an identifier usuallynamespace_package -->
                            <
type>model</type> <!-- singleton disabled -->
                            <class>
Sample_Module_Model_Observer</class><!-- observer class -->
                            <
method>coreModelStoreLoadAfter</method><!-- method -->
                        </
sample_module>
                    </
observers>
                </
core_store_load_after>
            </
events>

- I get a product and from the product I get the store view. Now from this store view object, can I access the object(model) of the new table I created. Remember, I have the store_id as foreign key in this table(refer the first question)? By accessing I mean not by load operation but by chaining getXXX();

Thanks for helping me out.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

If you do something like this:

$store Mage::getModel('core/store')->load($someStoreId);
then your observer should be called.

And one more thing...where did you put the code for the observer? What file?

 
Magento Community Magento Community
Magento Community
Magento Community
 
gokuljeyapaul
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2012-03-11
 

Hi Marius,

I have the Observer.php is in the path \"local/Sample/Module/Model/Observer.php\"

I was assuming that,

$store Mage::getModel(\'core/store\')->load($someStoreId);

is the same as accessing a store via the url.

For example - I was assuming that accessing http://localhost/magento/ would automatically load the (default) store and hence call the Sample_Module_Model_Observer. That was why I was wondering why the observer was not being called.

Thanks,
Gokul

 
Magento Community Magento Community
Magento Community
Magento Community
 
gokuljeyapaul
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2012-03-11
 

Just got the observer fixed, The following observer configuration works when the store is loaded by

$store Mage::getModel(\'core/store\')->load($someStoreId);

<events>
               <
store_load_after> <!-- event -->
                    <
observers> <!-- list of observers -->
                        <
sample_module><!-- an identifier usuallynamespace_package -->
                            <
type>model</type> <!-- singleton disabled -->
                            <class>
Sample_Module_Model_Observer</class><!-- observer class -->
                            <
method>coreModelStoreLoadAfter</method><!-- method -->
                        </
sample_module>
                    </
observers>
                </
store_load_after>
            </
events>

But the unanswered question is - when I access a http://localhost/magento/ the observer should be called. Why isn\’t this happening? Am I assuming something wrong here?

 
Magento Community Magento Community
Magento Community
Magento Community
 
gokuljeyapaul
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2012-03-11
 

Hi folks,

When is the store or store view loaded in Magento? I am still stuck here.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top