2013-02-28 86 views
0

中的所有行由於我需要提供JSON格式的產品評級,因此我在model文件夾中創建了一個帶有控制器和rating.php文件的模塊。我們運行控制器,它顯示了該表中的所有數據,但我只需要一行。所以通過url我傳遞一個參數,但它不會工作。我在這裏附上我的indexcontroller.php。就此建議我。Magento Controller顯示錶

<?php 
class Modulename_CustomRating_IndexController extends Mage_Core_Controller_Front_Action 
{ 

    public function indexAction() 
    { 

$arrParams = $this->getRequest()->getParams(); 

    var_dump($arrParams); 

$collection = Mage::getModel('Modulename_CustomRating_Model_CustomRating')->getCollection(); 
} 
print_r (json_encode($collection ->getData())); 



} 

} 

?> 

正如我傳遞的網址爲:本地主機/ Magento的/ customrating vote_id = 1,它走的是參數,但返回整個表的數據?我知道這是由於getData();但如何獲得所需的行?

+1

你應該看看addAttributeToFilter()函數並添加諸如 - > addAttributeToFilter('vote_id',$ arrParams ['vote_id'])到你的集合中。 – dagfr 2013-02-28 07:17:04

+0

我不明白。您是否想要只有兩列(產品編號,評級)或一行(產品編號過濾)的多行(所有產品)與該產品的整個數據? – enenen 2013-02-28 10:01:32

+0

@enenen我只想要一個按產品ID過濾的行。 – 2013-02-28 10:02:54

回答

1

您必須使用setEntityPkFilter方法。檢查Mage_Rating_Model_Resource_Rating_Option_Vote_Collection類別的其他方法。

$product_id = $this->getRequest()->getParam('product_id'); // or 'vote_id' 

$collection = Mage::getModel('Modulename_CustomRating_Model_CustomRating') 
       ->getResourceCollection() 
       ->setEntityPkFilter($product_id); 

如果你想只有1列,你可以嘗試一些Zend的東西,因爲你不能使用addAttributeToSelectgetSelect()返回Zend like查詢:

$adapter = $this->getConnection(); // $this->getConnection(); 

$collection->getSelect() 
      ->reset(Zend_Db_Select::COLUMNS) // remove all columns 
      ->columns('attribute_name'); // add only needed one 

$result = $adapter->fetchAll($select); 

不知道這是否可行。它沒有經過測試,只是一個想法。

+0

它返回致命的錯誤作爲調用未定義的方法..致命錯誤:調用未定義的方法Mage_Rating_Model_Resource_Rating_Option_Vote_Collection :: addAttributeToSelect()在/ var/www/html/magento /應用程序/代碼/本地/模塊名/ CustomRating/controllers/IndexController。第13行的PHP調用堆棧:0.0001 324872 1. {main}()/var/www/html/magento/index.php:0 0.0023 413312 – 2013-02-28 11:42:17

+0

嘗試使用'setEntityPkFilter'。編輯我的答案。 – enenen 2013-02-28 13:23:57

+0

是的,先生..它的工作,謝謝。但它會返回所有數據列。我如何限制其他列,如果我只想要百分比列,就好像我們給出PK值? – 2013-02-28 14:40:58