Magento - 如何从观察者模型中获取数据
Magento - How do I grab data from observer model
我正在尝试创建一个简单的产品选择器。来自表单的 post 数据被传输到控制器到观察者。如果我 var_dump 在观察者中,数据存在但是我无法在我的块 class.
上访问它
这是我的控制器:
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
问题:
我无法通过块 class 访问来自观察者的数据到 phtml,我不明白这个错误。
问题:
这个错误是什么意思?
如何通过块 class 访问来自观察者的数据到 phtml?
我的流程正确吗?
你能给我一些指导吗?
谢谢!
您尝试在块中获取产品,但没有数据。
我为您推荐更好的解决方案:
控制器:
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; ?>
这不应该由事件完成。
我正在尝试创建一个简单的产品选择器。来自表单的 post 数据被传输到控制器到观察者。如果我 var_dump 在观察者中,数据存在但是我无法在我的块 class.
上访问它这是我的控制器:
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
问题:
我无法通过块 class 访问来自观察者的数据到 phtml,我不明白这个错误。
问题:
这个错误是什么意思?
如何通过块 class 访问来自观察者的数据到 phtml?
我的流程正确吗?
你能给我一些指导吗?
谢谢!
您尝试在块中获取产品,但没有数据。
我为您推荐更好的解决方案:
控制器:
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; ?>
这不应该由事件完成。