2012-07-13 26 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

相关问题