2015-08-28 70 views
0

我是新来magento。我只想构建一个查询,但我正在努力做。Magento加入查询查询使用3表

我的MySQL查询:

SELECT `main_table`.*, `t1`.*, count(t2.review_id) AS `totalrecord`, SUM(t2.rating) AS `totalrating`, `t2`.* FROM `nbmp_vendor` AS `main_table` 
LEFT JOIN `customer_entity` AS `t1` ON main_table.customer_id = t1.entity_id 
LEFT JOIN `nbmp_review` AS `t2` ON main_table.customer_id = t2.vendor_id 
WHERE ((vendor_status = '1')) AND (t1.group_id = '4') group by main_table.vendor_id 

我曾尝试在Magento:

<?php 
    class Blazedream_TopVendors_Block_Monblock extends Mage_Core_Block_Template { 

public function methodblock() { 

    $myTable = "customer_entity"; 
    $myTable1 = "nbmp_review"; 
    $collection = Mage::getModel('topvendors/topvendors')->getCollection() 
          ->addFieldToFilter(array('vendor_status'), array('1')) 
          ->setOrder('vendor_id','asc'); 

    $collection->getSelect() 
      ->joinLeft(array("t1" => $myTable), "main_table.customer_id = t1.entity_id") 
      ->where("t1.group_id = '4'"); 

    $collection->getSelect() 
      ->columns('count(t2.review_id) AS totalrecord') 
      ->columns('SUM(t2.rating) AS totalrating') 
      ->joinLeft(array("t2" => $myTable1), "main_table.customer_id = t2.vendor_id") 
      ->group("main_table.vendor_id"); 
    echo $collection->getselect(); die; 
     $ddata = $collection->getData(); 
     $i = 0; 
     foreach($ddata as $data) 
     { 

     $retour[$i]['id'] = $data->getData('vendor_id'); 
     $retour[$i]['name'] = $data->getData('vendor_name'); 
     $retour[$i]['vendor_slug'] = $data->getData('vendor_slug'); 
     // $retour[$i]['rating_avg'] = $vendors[$data->getData('vendor_id')]['rating_avg']; 
     $retour[$i]['totalrating'] = $data->getData('totalrating'); 
     $retour[$i]['rating_avg'] = $data->getData('totalrating')/$data->getData('totalrecord'); 
     $i++; 

    } 
    // Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!'); 
    return $retour; 
} 
} 

我得到的唯一错误。

Fatal error: Call to undefined method Blazedream_TopVendors_Model_Mysql4_TopVendors_Collection::group() in magento 

任何人都可以帮助我形成? 在此先感谢。

+1

你能用错误信息更新你的问题吗? – Muk

+0

好的。只需一分钟。 – tttt

+0

我已更新..请参考错误的查询。 – tttt

回答

1

请试试以下内容,看看它是否对您有所帮助。

$myTable  = "customer_entity"; 
$myTable1 = "nbmp_review"; 
$collection = Mage::getModel('topvendors/topvendors')->getCollection() 
         ->addFieldToFilter(array('vendor_status'), array('1')) 
         ->setOrder('vendor_id','asc');       

$collection->getSelect()->group('vendor_id'); // TRY THIS LINE OF CODE 

$collection->getSelect() 
     ->joinLeft(array("t1" => $myTable), "main_table.customer_id = t1.entity_id") 
     ->where("t1.group_id = '4'"); 

$collection->getSelect() 
     ->columns('count(t2.review_id) AS totalrecord') 
     ->columns('SUM(t2.rating) AS totalrating') 
     ->joinLeft(array("t2" => $myTable1), "main_table.customer_id = t2.vendor_id"); 
+0

$ collection-> getSelect() - > group('main_table.vendor_id'); //这只是正确的..但我仍然无法检索和打印查看页面。 – tttt

+0

@tttt你在哪里运行这个查询? – Muk

+0

只在块中,等待我编辑代码。 – tttt