简体   繁体   中英

Magento: How to pass a variable from block to a Model

I am using an extension where I need to pass the variable is moving from block class back to model, I have used session it works sometime but not uniformly, Is there any other way to pass it,

Thanks in advance,

try using Magento's Registry Pattern

The three registry methods are

Mage::register   
Mage::unregister   
Mage::registry 

The register method is how you set a global-like variable.

Mage::register('some_name', $var);

Then, later in the request execution, (from any method), you can fetch your variable back out

$my_var = Mage::registry('some_name');

Finally, if you want to make you variable unavailable, you can use the unregister method to remove it from the registry.

Mage::unregister('some_name');

Source

If you can use the Model as a singleton, you can try this:

 Mage::getSingleton('yourmodule/yourmodel')->setStuff('xxxx');

and later

 Mage::getSingleton('yourmodule/yourmodel')->getStuff();

if you don't know what singletons are you should maybe try the registry approach from epynic to prevent problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM