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>
Layout XML
<?xml version="1.0"?>
<layout version="0.1.0">
<mdg_giftregistry_index_index>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block name="giftregistry.list" type="mdg_giftregistry/list" template="mdg/list.phtml" as="giftregistry_list"/>
</reference>
</mdg_giftregistry_index_index>
<mdg_giftregistry_index_new>
<reference name="content">
<block name="giftregistry.new" type="core/template" template="mdg/new.phtml" as="giftregistry_new"/>
</reference>
</mdg_giftregistry_index_new>
<mdg_giftregistry_index_edit>
<reference name="content">
<block name="giftregistry.edit" type="core/template" template="mdg/edit.phtml" as="giftregistry_edit"/>
</reference>
</mdg_giftregistry_index_edit>
<mdg_giftregistry_view_view>
<reference name="content">
<block name="giftregistry.view" type="core/template" template="mdg/view.phtml" as="giftregistry_view"/>
</reference>
</mdg_giftregistry_view_view>
<mdg_giftregistry_search_index>
<reference name="content">
<block name="giftregistry.search" type="core/template" template="mdg/search.phtml" as="giftregistry_search"/>
</reference>
</mdg_giftregistry_search_index>
<mdg_giftregistry_search_results>
<reference name="content">
<block name="giftregistry.results" type="mdg_giftregistry/list" template="mdg/list.phtml"/>
</reference>
</mdg_giftregistry_search_results>
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
<block type="mdg_giftregistry/add" name="registry.add" as="registry_add" template="mdg/add_to_registry.phtml" />
</reference>
</reference>
</catalog_product_view>
</layout>
Try this solution. Change this line
$this->getLayout()->getBlock('mdg_giftregistry.search.results')->setCustomerRegistries($results);
to
$this->getLayout()->getBlock('giftregistry.results')->setCustomerRegistries($results);