2

Hi I am encountering an error passing variables from controller to Block. I am trying to do a search function.

Error Message:

Fatal error: Call to a member function setCustomerRegistries() on a non-object in E:\xampp\htdocs\magento2\app\code\local\Mdg\Giftregistry\controllers\SearchController.php on line 27

controller

<?php
class Mdg_Giftregistry_SearchController extends Mage_Core_Controller_Front_Action{

    public function indexAction(){
        $this->loadLayout();
        $this->renderLayout();
        return $this;
    }
    public function resultsAction(){
        $this->loadLayout();

        if ($searchParams = $this->getRequest()->getParam('search_params')) {

            $results = Mage::getModel('mdg_giftregistry/entity')->getCollection();

            if($searchParams['type']){
                $results->addFieldToFilter('type_id', $searchParams['type']);
            }

            if($searchParams['date']){
                $results->addFieldToFilter('event_date', $searchParams['date']);
            }

            if($searchParams['location']){
                $results->addFieldToFilter('event_location', $searchParams['location']);
            }
            $this->getLayout()->getBlock('mdg_giftregistry.search.results')->setCustomerRegistries($results);
        }

        $this->renderLayout();
        return $this;
    }


}

Block

<?php
class Mdg_Giftregistry_Block_List extends Mage_Core_Block_Template
{
    public function getCustomerRegistries()
    {
        $collection = null;
        $currentCustomer = Mage::getSingleton('customer/session')->getCustomer();
        if($currentCustomer)
        {
            $collection = Mage::getModel('mdg_giftregistry/entity')->getCollection()
                ->addFieldToFilter('customer_id', $currentCustomer->getId());
        }
        return $collection;
    }
}

Template

<?php
$_collection = $this->getCustomerRegistries();
$helper = Mage::helper('mdg_giftregistry')
?>
<div class="customer-list">
    <?php if(!$_collection->count()): ?>
        <h2><?php echo $this->__('You have no registries.') ?></h2>
        <a href="<?php echo $this->getUrl('giftregistry/index/new') ?>">
            <?php echo $this->__('Click Here to create a new Gift Registry') ?>
        </a>
    <?php else: ?>
        <ul>
            <?php foreach($_collection as $registry): ?>
                <li>
                    <h3><?php echo $registry->getEventName(); ?></h3>
                    <p><strong><?php echo $this->__('Event Name:') ?> <?php echo $registry->getEventName(); ?></strong></p>
                    <p><strong><?php echo $this->__('Event Location:') ?> <?php echo $registry->getEventLocation(); ?></strong></p>
                    <a href="<?php echo $this->getUrl('giftregistry/view/view', array('_query' => array('registry_id' => $registry->getEntityId()))) ?>">
                        <?php echo $this->__('View Registry') ?>
                    </a>
                    <?php if($helper->isRegistryOwner($registry->getCustomerId())): ?>
                        <a href="<?php echo $this->getUrl('giftregistry/index/edit', array('_query' => array('registry_id' => $registry->getEntityId()))) ?>">
                            <?php echo $this->__('Edit Registry') ?>
                        </a>
                        <a href="<?php echo $this->getUrl('giftregistry/index/delete', array('_query' => array('registry_id' => $registry->getEntityId()))) ?>">
                            <?php echo $this->__('Delete Registry') ?>
                        </a>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</div>
  • 2
    Could you add your layout.xml that define mdg_giftregistry.search.results? – Renon Stewart Jun 24 '15 at 15:46
3

You can use register function in magento.

Try this:

In controller

$data = 'your data that you want to pass to block';
Mage::register('any_name', $data);

Now in your block

Mage::registry('any_name')

PS any_name is just name given to hold that data and which should be same while calling it in block

2

Simple use registry variable.

Set a registry variable at controller

Mage::register('YourVairbleName',$value);

Get data on block file by

Mage::registry('YourVairbleName');

See details at http://alanstorm.com/magento_registry_singleton_tutorial

  • is that the best way to do it? any mag setter/getter approach? I am new to magento – tom_cruz Jun 24 '15 at 10:46
  • yes.. this is best solution as per as magento logic.... – Amit Bera Jun 24 '15 at 10:48
2

You could use register as noted in the other answers to accomplish your goal. However you method above should also work. The error is stating that your block is not defined.

Technically you should have done something like (which still won't work because the block does not exist).

 $block = $this->getLayout()->getBlock('mdg_giftregistry.search.results');
 if($block){
     $block->setCustomerRegistries($results);
 }

Below is an example of core magento doing the same thing you are doing. see app/code/core/Mage/Contacts/controllers/IndexController.php

public function indexAction()
{
    $this->loadLayout();
    $this->getLayout()->getBlock('contactForm')  <-----
         ->setFormAction( Mage::getUrl('*/*/post') );

app/design/frontend/base/default/layout/contacts.xml

   <contacts_index_index translate="label">
        ......
        <reference name="content">
            <block type="core/template" name="contactForm" template="contacts/form.phtml"/>
                                        --------^
        </reference>
    </contacts_index_index>

Double check your block name/Alias

Also you may need to change your block logic slight

<?php
class Mdg_Giftregistry_Block_List extends Mage_Core_Block_Template
{
    $collection = NULL;

    public function setCustomerRegistries($result)
    {
        $this->collection = $result;
    }

    public function getCustomerRegistries()
    {
        if($this->collection){
           return $this->collection;
        }

        $currentCustomer = Mage::getSingleton('customer/session')->getCustomer();
        if($currentCustomer)
        {
            $collection = Mage::getModel('mdg_giftregistry/entity')->getCollection()
                ->addFieldToFilter('customer_id', $currentCustomer->getId());
        }
        return $collection;
    }
}

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.