2012-07-13 60 views
0

我有一個產品Id的列表,並基於該產品Id我想獲取產品名稱,但由於catalog_product_entity表不包含產品名稱,只有sku,我被困住了。使用Collection Magento獲取ProductName

查收我的代碼,我已經試過左右加入兩個,但如果你想你的收藏裝載的產品名稱沒有得到名稱

$collection = Mage::getModel('catalog/product')->getCollection() 
    ->addAttributeToSelect('name') 
    ->getSelect()->joinRight( 
     array('table_alias'=>'my_table'), 
     'entity_id = table_alias.product_id', 
     array('table_alias.*') 
    ); 

$result = null; 

foreach ($collection as $temp) {  
    $result[] = array(
     'name' => $temp->getCustomer_name(),    
    ); 
} 
+0

,如果你想獲得產品名稱和你的產品ID,以便使用產品ID只加載的產品,你會得到所有的產品信息 – Mufaddal 2012-07-13 12:24:49

+0

好吧,假設我有100個產品我將運行100個查詢以獲取每個產品的產品名稱,但這不是針對每個產品運行查詢的好方法Id – MZH 2012-07-13 12:34:12

回答

0

這是我如何解決它

$resource  = Mage::getSingleton('core/resource'); 
       $readResource = $resource->getConnection('core_read'); 
       $tableName  = $resource->getTableName('myTable'); 

       $results = $readResource->select('table_alias.*') 
       ->from($tableName) 
       ->joinLeft(
           array('table_alias'=>'catalog_product_flat_1'), 
           'product_id = table_alias.entity_id', 
           array('table_alias.*') 
          )->where('customer_id=?',$customerId); 

       $products=$readResource->fetchAll($results); 

       $dataList = array(); 

       foreach($products as $row) 
       { 
        $dataList[] = array('productName'=>$row['name'],'price'=> $row['price'],'status'=>$row['status'],'myTable_id'=>$row['myTable_id']); 
       } 
0

,那麼你的代碼的第一部分是對的。下面是一個簡化版本:

$collection = Mage::getResourceModel('catalog/product_collection') 
        ->addAttributeToSelect('name'); 

$result = array(); 

foreach ($collection as $temp) { 
    $result[] = array('name' => $temp->getName()); 
} 
+0

對不起,但我需要產品名稱acc to my_table,u錯過了加入 – MZH 2012-07-13 13:45:48

+0

我不清楚爲什麼你加入外部表來獲取產品名稱。我猜這不是Magento提供的默認產品名稱屬性,而是您定製的其他東西,對吧? 同樣在您的代碼中,您正在訪問一個名爲customer name的屬性,而不是實際的產品名稱。感覺有一些信息缺失 – barbazul 2012-07-14 16:46:33

+0

不,實際上我已將productId和customerId保存在myTable中,現在我想顯示保存在myTable中的那些productIds的名稱,以便將其與目錄/產品表一起加入 – MZH 2012-07-15 06:46:14