您好我遇到从控制器传递变量到块的错误。我正在尝试搜索功能。
错误消息:
致命错误:在a上调用成员函数setCustomerRegistries() 非对象 E:\ XAMPP \ htdocs中\ magento2 \程序\代码\本地\千年发展目标\礼物登记\控制器\ SearchController.php 在第27行
控制器
<?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;
}
}
阻止
<?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;
}
}
模板
<?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>
布局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>
答案 0 :(得分:1)
试试这个解决方案。改变这一行
$this->getLayout()->getBlock('mdg_giftregistry.search.results')->setCustomerRegistries($results);
到
$this->getLayout()->getBlock('giftregistry.results')->setCustomerRegistries($results);