我试图创建一个简单的产品选择器。来自表单的发布数据被转移到控制器到观察者。如果我在观察者中使用var_dump,则数据存在,但是我无法在我的块类中访问它。
这是我的控制者:
public function indexAction() {
$session = Mage::getSingleton('core/session');
if ($data = $this->getRequest()->getPost()) {
$data['ta'] = $this->getRequest()->getPost('torqueAction');
$data['tr'] = $this->getRequest()->getPost('torqueRequired');
$data['tm'] = $this->getRequest()->getPost('torqueMetric');
$eventdata = $data;
Mage::dispatchEvent('selector_custom_event', $eventdata);
}
$this->_redirect('prodselector/index/result');
}
public function resultAction() {
$this->loadLayout();
$this->renderLayout();
}
这是我的观察员
public function observe($observer) {
$event = $observer->getEvent();
$test = $event->getData();
return $test;
}
这是我的阻止:
public function getSelectedProducts() {
$arr_products = array();
$products = Mage::getModel("prodselector/observer")->observe();
return $arr_products;
}
这是我在phtml上测试的方式:
<?php var_dump($this->getSelectedProducts()); ?>
每次执行模块时都出错了
Fatal error: Call to a member function getEvent() on a non-object in app/code/local/Rts/Prodselector/Model/Observer.php on line 6
问题:
我无法通过块类访问观察者的数据到phtml,我也不理解错误。
问题:
此错误的含义是什么?
如何通过块类从观察者访问数据到phtml?
我的流程是否正确?
你能给我一些指导吗?谢谢!
答案 0 :(得分:0)
您尝试在块中获取产品,但没有数据。
我建议您使用更好的解决方案:
控制器:
public function indexAction() {
$session = Mage::getSingleton('core/session');
if ($data = $this->getRequest()->getPost()) {
$ta = $this->getRequest()->getPost('torqueAction');
$tr = $this->getRequest()->getPost('torqueRequired');
$tm = $this->getRequest()->getPost('torqueMetric');
$productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('torque_action', $ta)
->addFieldToFilter('torque_required', $tr)
->addFieldToFilter('torque_metric', $tm);
Mage::getSingleton('core/session')->setTorqueProductCollection($productCollection);
}
$this->_redirect('prodselector/index/result');
}
public function resultAction() {
$this->loadLayout();
$this->renderLayout();
}
在街区:
public function getSelectedProducts() {
$products = Mage::getSingleton('core/session')->getTorqueProductCollection();
return $products;
}
在模板中:
<?php foreach ($this->getSelectedProducts() as $product) : ?>
<?php print_r($product->getName());
<?php endforeach; ?>
这不应该由事件来完成。