Let's say I have a custom Admin module that simply checks the magento database for cache settings. I want to pull the results to the page I created in admin.

I have a view in /design/adminhtml/default/default/templates/upgradecheck/view.phtml I have the layout file in /design/adminhtml/default/default/layout/upgradecheck.xml

and then I have my custom module (Myname_Upgradecheck).

Everything "works" - as in when I load that page, it displays exactly the text I would expect (e.g. "1 cache is enabled"). My question is, did I do this correctly? Should the logic be in another file that just returns how many cache types are enabled and then call that function from the block? How exactly would I do that if that needs to be done?

class Myname_Upgradecheck_Block_Adminhtml_Upgradecheck extends Mage_Adminhtml_Block_Template {
public function showCacheCount()
    $cachecheck = Mage::getModel('upgradecheck/cachecheck')->getCollection();
    $check = array();
    foreach($cachecheck as $cache){
        if ($cache->getValue() != 0) {
            array_push($check, $cache->getCode());
    if (count($check) != 0) {
        return count($check) . ' caches are enabled';
    return 'No cache types are enabled';


In my view.phtml file I just have: echo $this->showCacheCount();


This looks somehow OK. It works but it's not very clean.
Since you need only the number of enabled caches I would do it a bit differently (does not mean I'm doing it right).

I assume that upgradecheck/cachecheck is a custom model that maps on a table and that table has a column called value.
In this case you don't need to load the full collection and check each entity for the value field.
You can get directly the number of records with value not 0 like this:

class Myname_Upgradecheck_Block_Adminhtml_Upgradecheck extends Mage_Adminhtml_Block_Template {
    public function getEnabledCacheCount()
        $cachecheck = Mage::getModel('upgradecheck/cachecheck')->getCollection();
        $cachecheck->addFieldToFilter('value', array('neq' => 0)); //this will filter only the records with value != 0.
        //now you can get directly the size of the collection;
        return $cachecheck->getSize();

And in your template you can do this:

<?php $enabledCacheCount = $this->getEnabledCacheCount() ?>
<?php if ($enabledCacheCount > 0) : ?>
    <?php echo $this->__('%d caches are enabled', $enabledCacheCount)?> <!-- make use of the translation function -->
<?php else : ?>
    <?php echo $this->__('No cache types are enabled')?>
<?php endif;?>

I know that the template in this form contains only php code, but now you can easily add some markup around the messages. for example a <span class="green"> for when there are caches enabled and <span class="red"> for when there are no caches enabled.
This way you separate the logic from the presentation.
If you have a designer that does not have much knowledge of PHP or Magento you can simply point him/her to this file and tell him/her to do his/her magic.
If you output everything from a class method someone might get confused.

  • Thank you for this answer, it explained exactly what I wanted to know and went a step further to help me with the view. Jan 6 '15 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.